The awk pattern scanning and processing language is frequently used in order to process lines that match a given criteria. Here we present several approaches to filtering with criteria.

For the sake of discussion let's assume we have a file named file.txt with the following content:

(2,3)
text
(4,90)
(hello)
comment
()
finish

And we want to show the lines that start with a "(" symbol preceding them with the word: "Parenthesis" so that we obtain the following output:

Parenthesis (2,3)
Parenthesis (4,90)
Parenthesis (hello)
Parenthesis ()

We know that our pattern for filtering is "^(" but there are various ways that we can choose to apply it.

Filter using grep

Using grep is what comes to mind when you are in a hurry or your knowledge of awk is limited:

grep "^(" file.txt | awk '{print "Parenthesis " $0}'

Filter using a conditional in awk

awk is a very powerful language which can be used for a variety of tasks (scripting support for programming, OS administration automation, text editing, etc.).

As is the case with most languages, awk offers an if-else construction:

awk '{ if (index($0,"(")==1) { print "Parenthesis " $0} }' file.txt

The advantage we obtain here over the previous option is that awk itself is able to perform the both the filter and the print and since only one command is required we don't need to pipe the output from one into the input of the other.

A disadvantage of this approach would be that as further code is added inside the awk script it rapidly begins to increase in complexity and loose readability.

awk filters

Fortunately, we can combine both a grep style filter and an awk print statement within the awk command itself by using its' built-in filters.

In the following script, we use a single awk command to perform both functions while maintaining a compact, readable syntax:

awk '/^\(/ {print "Parenthesis " $0}' file.txt
Filed under: languages, tools

comments

There are no comments.