Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I got an awk script named my_awk:

#!usr/bin/awk -f

if ($1 == "#START") { FS=":";} 
else if ($1 == "#STOP") { FS = " ";}
else { print $3} 

And I invoked it through:

cat my_file | awk -f my_awk

I'm new to awk so I'm ignorant about awk's mechanism. Will this my_awk script execute its command to the whole my_file or to each line of the file?

share|improve this question
up vote 2 down vote accepted

When you pipe something to a script, the script will only be executed once. So, for example if you do:

foo | bar

then bar is called only once, with its STDIN being whatever foo wrote to STDOUT.

You have a useless use of cat there, since you could just do:

awk -f my_awk < my_file

Or, since awk can directly work with file name arguments:

awk -f my_awk my_file

If you go further, awk itself is a tool that works on line-by-line basis, but it's really called only once by the shell. From this tutorial:

Like most UNIX utilities, AWK is line oriented. That is, the pattern specifies a test that is performed with each line read as input.

share|improve this answer
but how awk works of the file? The script I wrote above has successfully changed the FS separator for 2 different lines! – Zen Jun 18 '14 at 2:29
See my updated answer. – slhck Jun 18 '14 at 5:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .