Tuesday, June 3, 2008

What AWK can do for Linux.

In Linux, there are many tools to process text and other forms of data. One tool that comes to mind is the AWK. The AWK scripting processes a file line by line and can produce a summary of its finding as header or footer.

TLDP provides a guide on AWK at http://tldp.org/LDP/abs/html/awk.html

Here is a demonstration of the AWK to extract the directories mounted.
STEP 1. Create an AWK script file called "myscript1.awk". This should contain two lines.

BEGIN{ printf("Display the third field\n"); }
{ print $3 }

STEP 2. Run the following command to use the AWK script.

mount -t ext2,ext3 | awk -f myscript1.awk

Without using an AWK script file, extract data from the file /etc/passwd and display the results to screen.

STEP 1. Run the following command

awk -F: '{ print $1 ":" $3 ":" $7 }' /etc/passwd

AWK have been used for more complex reporting but the above should give an idea of how this is done.


surface said...

the alternative of awk can be cut, cut provides simple way to extract data by columns, but recently I tend to use python to do the, I find the list in python are more powerful.

Tboxmy said...

AWK is a lighter and have been available in all Linux. It is also part of the LPI test.