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!

The Linux Command Line

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

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad