04. Patterns and Actions print

In the previous lesson, we looked at records & fields, and saw how AWK is able to parse and manipulate them. While learning this, you may have noticed that each action performed is enclosed within braces, which then applies to all records.

Patterns

But what if we just wanted to apply actions to lines that matched a specific pattern? We can do this by preceding actions by a regular expression pattern.

/pattern/ { action }     // Action is applied only those records that match pattern 
/pattern/                // Print all lines matching /pattern/
{ action }               // Apply actions on all lines

This allows us to select which lines to apply our actions to. If you do not specify a pattern, then the action is applied to all lines. On the other hand, if there is no action, then all lines with the specified pattern are printed out.

Actions

Actions tell AWK how to process a specific record or part of its fields. Let's look at the print action, as it's the most basic thing you can do with a record.

Printing

When print is called, it will print out the record with an output record separator (ORS), the default of which is a newline character. In the following example, all record will be printed. We have already seen how we can specify the entire record (with $0) and the specific field n ($n) with the dollar symbol.

$ echo ' uno dos tres ' | gawk -F' ' '{ print $0 }'
uno dos tres
# Default is to print the record
$ echo ' uno dos tres ' | gawk -F' ' '{ print }'
 uno dos tres
# Print a specific field only
$ echo ' uno dos tres ' | gawk -F' ' '{ print $2 }'
dos

Printing by pattern

Now we can follow a certain pattern and print only those that match. For this example, we'll use grades.txt, which is a file containing grade reports of five students.

$ gawk '$6 ~ /B/ { print $0 }' grades.txt
Vern    Wynne  85     78     93     B
Ingram  Dannie 84     85     94     B+
Johnnie Adair  78     94     87     B
# Print last name, first name for students with a B
$ awk '/B/ {print $2 "\t" $1}' grades.txt 
Wynne Vern
Dannie  Ingram
Adair Johnnie

Here, we can use the ~ to select those that match field #6.

That's all for now...

This was just part 1 of our Awk series. If you're interested in learning more, please follow us on Twitter or Like us on Facebook for our next update!

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad