I'd like to do the following transformation within a file (named inputfile) with sed
Searched line :
$CmdVar $ENV\util.exe firstparm secondparm
Searched pattern is util.exe ; firstparm and secondparm are always the last two words. The job consists in commenting the matching whole line with ###, a tag (mytag), ###, the searched pattern, ###, and the two parameters followed by ### again.
So expected output would be :
###mytag###util.exe###firstparm secondparm### $CmdVar $ENV\util.exe firstparm secondparm
i'm thinking of combining some grep/awk and a loop in order to collect relevant information to use sed by specifying line number.
info=`grep -n "util.exe" inputfile | awk '{print $1,$(NF-1),$NF","}'` if [ ! -z $info ]; then tr , '\n' <<<"$info" | while read; do words=${REPLY} # use sed here by splitting $words done fi
Because of the high number of files i have to handle, this might take a lot of time.
Is there any a more straightforward way to use sed or awk to achieve this? Thanks in advance
If query is a regex:
awk '
$0 ~ q {
printf "###%s###%s###%s %s### ",
tag, q, $(NF-1), $(NF)
}
{ print }
' tag='mytag' q='util.exe' inputfile >outputfile
If query is a plain string:
awk '
index(q) {
printf "###%s###%s###%s %s### ",
tag, q, $(NF-1), $(NF)
}
{ print }
' tag='mytag' q='util.exe' inputfile >outputfile