How cool is Linux AWK

Awk is geared toward text processing and report generation, yet features many well-designed features that allow for serious programming. And, unlike some languages, awk’s syntax is familiar, and borrows some of the best parts of languages like C, python, and bash (although, technically, awk was created before both python and bash). Awk is one of those languages that, once learned, will become a key part of your strategic coding arsenal.

Let’s go ahead and start playing around with awk to see how it works.


The essential organization of an AWK program follows the form:

The pattern specifies when the action is performed. Like most UNIX utilities, AWK is line oriented. That is, the pattern specifies a test that is performed with each line read as input. If the condition is true, then the action is taken. The default pattern is something that matches every line.

See the example here:

You should see the contents of your /etc/fstab file appear before your eyes.


When we executed awk, it evaluated the print command for each line in /etc/fstab , in order. All output is sent to stdout, and we get a result identical to catting /etc/fstab.

This time we will do the same thing but with a different awk syntax:

The $0 variable represents the entire current line, so print and print $0 do exactly the same thing.


Next example will replace the lines with a string of your choice:


Breaking Text

Awk is really good at handling text that has been broken into multiple logical fields, and allows you to effortlessly reference each individual field from inside your awk script. The following script will print out a list of all user accounts on your system:

When we called awk, we use the -F option to specify “:” as the field separator. When awk processes the print $1command, it will print out the first field that appears on each line in the input file.

More strings

As you can see, awk prints out the first and third fields of the /etc/passwd file, which happen to be the username and uid fields respectively.


Strings with spaces in between

When you call print this way, it’ll concatenate $1, " ", and $3, creating readable output.

Adding test to the combination

This will become readable output concatenated with the extra text.


This ends of first AWK tutorial.  See next tutorial on Using AWK Scripts




I hope this was helpful and i appreciate if you would like, subscribe or share our posts. I it is just a click


Similar Article  Instalando MySQL 5.6 no Linux CentOS 6.2
No Comments
Leave a Comment

%d bloggers like this: