write awk command into file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write awk command into file using awk
# 1  
Old 11-08-2010
write awk command into file using awk

hi,

i want to write my nawk command into a file. i want to write:
Code:
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:
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 Smilie Smilie

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

Last edited by gc_sw; 11-08-2010 at 09:15 AM..
# 2  
Old 11-08-2010
Code:
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

Smilie
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-09-2010
thank you Scrutinizer Smilie
# 4  
Old 11-10-2010
but actually i have to write it down in a single command. is there any way of to do it in single line command:

Code:
awk ...

# 5  
Old 11-10-2010
Technically it is a single command, though. It is equivalent to:
Code:
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?)
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-10-2010
actually; my code is:
Code:
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
# 7  
Old 11-10-2010
Try this:
Code:
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

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - read from a file and write conditional output

I have a file, which has '|' as separator; I need to read each line from that file and produce output to another file. While reading, I have certain condition on few specific columns (like column3 ='good'); only those lines will be processed. (3 Replies)
Discussion started by: mady135
3 Replies

2. Shell Programming and Scripting

Search text beween tags and write to file using awk

Hi Friends, I have a very big text file, that has code for multiple functions. I have scan through the file and write each function in seperate file. All functions starts with BEGIN DSFNC Identifier "ABCDDataValidationfnc" and ends with END DSFNC I need create a file(using identifier)... (2 Replies)
Discussion started by: anandapani
2 Replies

3. Shell Programming and Scripting

Compare two files and write data to second file using awk

Hi Guys, I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data. Example: Delimited File -------------- Byer;Amy;NONE1;A5218257;E5218257 Byer;Amy;NONE1;A5218260;E5218260 Positional File... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

4. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. Shell Programming and Scripting

How to write an awk script that emulates the wc command

How to write an awk script that emulates the wc command Thank you very much!:) (3 Replies)
Discussion started by: vcnvcn
3 Replies

7. Shell Programming and Scripting

Write Awk output to a file , inside script.

Hi, Can anyone please help me with this issue. I have a Awk command which take file as input, and provides the output having multiple lines, its working in command mode, but not if i plug it in script. #!/bin/ksh infile=a.txt outfile=b.txt awk ' BEGIN{ FS=OFS="|";ORS = "\n";... (1 Reply)
Discussion started by: sp999
1 Replies

8. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

9. UNIX and Linux Applications

Input a variable and write to a file using awk

Hi I am trying to edit a csv file. Bacically I need to input a search variable and the value that must be changed in one of the fields corresponding to that searched variable. My csv file looks like so: 1,1A,5 1,1B,2 1,1C,3 2,2A,7 2,2B,4 2,2C,0 3,3A,1 3,3B,6 3,3C,4 I want to... (4 Replies)
Discussion started by: ladyAnne
4 Replies
Login or Register to Ask a Question