Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute another awk from awk file

Tags:

awk

Is it possible to execute another awk file from a awk file? Using an awk file I need to execute all awk files in a current folder. Is it possible to do such operations in awk?

like image 528
suraj Avatar asked Nov 22 '12 11:11

suraj


People also ask

How do I run awk command specified in a file?

In order to tell awk to use that file for its program, you type: awk -f source-file input-file1 input-file2 … The -f instructs the awk utility to get the awk program from the file source-file (see Command-Line Options). Any filename can be used for source-file .

Can we use awk inside awk?

No. NR==0 will NEVER be true. Please show us a sample of your input file and tell us in English what you are trying to extract from that file into the mail message you're trying to send. The condition will not be effective inside awk since data is piped to sendmail regardless.

How do I run a command in awk?

Call External Command From awk awk + cp: Read input of a file list, and copy the files to a required destination with a defined name pattern. awk + md5sum: Read input containing a list of filenames, output the filename and the MD5 hash of the file.


1 Answers

Yes you can. You'll need to use the system() function. I'm assuming you only want to run these scripts once. If so, you can add them to the BEGIN block of your wrapper script:

BEGIN {
    system("awk -f ./script1.awk")
    system("awk -f ./script2.awk")
    system("awk -f ./script3.awk")
}

If you have a large number of scripts that need to be executed, you can use a for loop. Do make sure that your wrapper script isn't in the same directory as all the other awk scripts you'd like executed, or it will be included in the glob of awk scripts...

BEGIN {
    system("for i in *.awk; do awk -f \"$i\"; done")
}
like image 149
Steve Avatar answered Dec 29 '22 00:12

Steve