write awk command into file using awk

hi,

i want to write my nawk command into a file. i want to write:

awk '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/gc_sw/again.mos'"'"'

into gc.mos file. this is my code:

awk '{print "awk '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/gc_sw/again.mos'"'"'"}' > /home/gc_sw/gc.mos

but it fails :frowning: :frowning:

the code stops at special characters. i think we should use ASCII, or something like that instead of ' " } etc

awk 1 <<"EOF" > /home/gc_sw/again.mos
awk '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/gc_sw/again.mos'"'"'
EOF

:smiley:

1 Like

thank you Scrutinizer :slight_smile:

but actually i have to write it down in a single command. is there any way of to do it in single line command:

awk ...

Technically it is a single command, though. It is equivalent to:

cat <<"EOF" > /home/gc_sw/again.mos
awk '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/gc_sw/again.mos'"'"'
EOF

Why would you need this, gc_sw ? (It seems a bit cumbersome and unnecessary, no?)

1 Like

actually; my code is:

nawk '{print "l ./gc_sw "$1" '"'"'lt all;l+;lset SectorPort="$2" SectorPortNum "$3";l-;l nawk 'NR==14 && $NF!="Set."{print "l ./gc_sw "r" '"'"'lt all;"p"'"'"'"} NR==5{r=$2} NR==3{p=$2 FS $3 FS $4 FS $5}' $logfile > /home/gc_sw/again.mos'"'"'"}' /home/gc_sw/2.txt >> /home/gc_sw/por.mos

the black code itself is normally being written into file. but the red one is not. Because it is being considered as a part of original nawk code but it is not a part of first awk.

So i have to implement it in single way

Try this:

awk -v q="'" 'BEGIN{d="\""; printf "%s\n","awk "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/gc_sw/again.mos" q d q d q }' > /home/gc.mos
1 Like