Skip to content

Awk or bash

The short answer - keep using bash but have a look at of some awk neat features that might come in handy.

cmd= "ls -lrt" - lets have the command stored for convenience

system(cmd) - will make a system call and execute the command, while sending the output to standard output

cmd | getline - will do the same but you will read the first line from the output.

# then let's read all lines and do something
while (cmd | getline) {    
   if ($5 > 200) system("cp "$9" target_folder/")

Essentially, one gets an extremely easy way to communicate between programs and execute system calls in addition to convenient arithmetic functions (for example, bash does not like fractional numbers) and you add a lot of flexibility to your decision making script...

Have a look at the script that I wrote some 100 years ago that prepares a Gnuplot input to fit Birch–Murnaghan equation of state, then gets the results from the fit (without using temporary files) and prettifies a little bit the final plot. The latest Gnuplot can actually do this internally, but have a look at what one can do with awk.

Awk and multiple files

Something that awk is really good at is handling of multiple files, as bash does, but combined with all the tools that comes with the language. Look at this Case study Multiple files - first approach to get an idea why awk could be a better choice in such situation.

Awk inside bash script

Perhaps, a better idea is to add transparently an awk code into your bash script. Here are simple examples that illustrates two different ways to do it.


awk '
  BEGIN {print "Hi 3"}
' /etc/issue

echo "==================================="
Note that with the << EOF ... EOF implementation needs the $ sign needs to be escaped, otherwise bash will replace it with the content of the shell variable it refers to. This allows you to mix bash and awk in somewhat dangerous way...


awk -f - << EOF /etc/os-release
  BEGIN {print "Hi"}
  {print \$0}

echo "==================================="