append "awk command" to the end of each line

hi;
this is my qqq.mos:

l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-'
l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-'
l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-'
...

i want to append;

l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos

to the end of each line and i want to see:

l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos'
l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos'
l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos'
...

it is hard to write to a file an awk code. i am using:

l nawk -v q="'" 'BEGIN{d="\""; printf "%s\n","l nawk "q"NR==14 && $NF!="d"Set."d"{print "d"l ./gcsw "d"r"d" "q d q d q"lt all;"d"p"d q d q d q d"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}"q" $logfile > /home/gcsw/again.mos" }' > /home/gcsw/qqq.mos

to write it in single way but donot know how to append it to the end of every line. :(:confused::(:frowning:

thanks..

# cat input_file
l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-'
l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-'
l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-'
# cat awk_cmd
l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos
# awk ' NR == FNR  { str=$0;next } { print $0 ";" str } ' awk_cmd input_file
l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-';l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos
l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-';l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos
l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-';l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gcsw/again.mos

thanks anbu23 but;

cat: cannot open awk_cmd

:frowning:

store the awk command in a variable and use sed as

cat input_file
l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-'
l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-'
l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-'

VAR="l nawk 'NR==14 && .."
sed "s/.*/& $VAR/g" input_file > outfile
1 Like

thx michael, it is OK :slight_smile: