Saturday, May 28, 2011

AWK programming model

awk is usually input driven .i.e. it executes the command for the no of lines in the file supplied or the no of lines which is piped to the awk command.

For example:

acer@ubuntu:~$ cat > fff
ewrrwer
ewrw
wer
werwe
ewrwer^Z
[1]+  Stopped                 cat > fff
acer@ubuntu:~$ awk '{ print "hello world" } ' fff
hello world
hello world
hello world
hello world
hello world
acer@ubuntu:~$

Here the hello world is printed  5 times since fff has 5 lines.

There is a exception to it you can use BEGIN or END command which executes without waiting for the input provided by the file or the pipe.

In case of BEGIN command in awk executes before the input from the file is processed.

For example:
acer@ubuntu:~$ cat ggg
hello life
wonderful
great
marvellous
awesome
acer@ubuntu:~$
acer@ubuntu:~$ awk 'BEGIN { print "hello from begin"}  
> /wonderful/ { print "hello during file process" }'  ggg    
hello from begin 
hello during file process
acer@ubuntu:~$

PROCESS IN AWK passes through three simple steps:

It executes the BEGIN COMMAND before any input is read.
                                     |
It process the file or input for the no of lines in file.(main loop)
                                     |
It executes the END command after the main loop ends.

Here the BEGIN and END are optional.

And a note about the END command it does not get executed if there is no lines in the file or input and it waits for input unlike BEGIN command.

acer@ubuntu:~$ awk 'END { print "hello from end command" }'
it waits endlessly

When you provide a input , it executes
acer@ubuntu:~$ echo "hello" | awk 'END { print "hello from end command" }'
hello from end command
acer@ubuntu:~$

These in combination can be used for writing many useful commands.

For example : for now we will write a shell program to print the no of lines in the file

acer@ubuntu:~$ awk 'BEGIN { x=0 }
> { x=x+1 }   # for adding the no of lines
> END { print "no of lines in the file is" x }' ggg
no of lines in the file is5
acer@ubuntu:~$


Hope this clarifies your doubt about AWK PROGRAMMING MODEL.

Kindly comment


No comments:

Post a Comment