Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an executable program using awk

Tags:

shell

awk

I have a program in C that I want to call by using awk in shell scripting. How can I do something like this?

like image 519
user2030431 Avatar asked Jan 31 '13 20:01

user2030431


People also ask

How do you call a function in awk?

You just can't call shell functions from awk. You have to write an equivalent awk function, e.g. add this to your awk script: function dots(n) {while(n-- > 0) print "."}

How does awk command work with files?

Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform the associated actions. Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan.

How do I run an awk script in Windows?

You can download and run the setup file. This should install your AWK in " C:\Program Files (x86)\GnuWin32 ". You can run the awk or gawk command from the bin folder or add the folder ``C:\Program Files (x86)\GnuWin32\bin to your PATH`.


2 Answers

From the AWK man page:

 system(cmd)               executes cmd and returns its exit status 

The GNU AWK manual also has a section that, in part, describes the system function and provides an example:

system("date | mail -s 'awk run done' root") 
like image 172
Caleb Avatar answered Oct 11 '22 15:10

Caleb


A much more robust way would be to use the getline() function of GNU awk to use a variable from a pipe. In form cmd | getline result, cmd is run, then its output is piped to getline. It returns 1 if got output, 0 if EOF, -1 on failure.

First construct the command to run in a variable in the BEGIN clause if the command is not dependant on the contents of the file, e.g. a simple date or an ls.

A simple example of the above would be

awk 'BEGIN {     cmd = "ls -lrth"     while ( ( cmd | getline result ) > 0 ) {         print result     }     close(cmd); }' 

When the command to run is part of the columnar content of a file, you generate the cmd string in the main {..} as below. E.g. consider a file whose $2 contains the name of the file and you want it to be replaced with the md5sum hash content of the file. You can do

awk '{ cmd = "md5sum "$2        while ( ( cmd | getline md5result ) > 0 ) {             $2 = md5result        }        close(cmd);  }1' 

Another frequent usage involving external commands in awk is during date processing when your awk does not support time functions out of the box with mktime(), strftime() functions.

Consider a case when you have Unix EPOCH timestamp stored in a column and you want to convert that to a human readable date format. Assuming GNU date is available

awk '{ cmd = "date -d @" $1 " +\"%d-%m-%Y %H:%M:%S\""         while ( ( cmd | getline fmtDate) > 0 ) {             $1 = fmtDate        }        close(cmd); }1'  

for an input string as

1572608319 foo bar zoo 

the above command produces an output as

01-11-2019 07:38:39 foo bar zoo 

The command can be tailored to modify the date fields on any of the columns in a given line. Note that -d is a GNU specific extension, the *BSD variants support -f ( though not exactly similar to -d).

More information about getline can be referred to from this AllAboutGetline article at awk.freeshell.org page.

like image 37
Inian Avatar answered Oct 11 '22 17:10

Inian