How to Use Awk and Regular Expressions to Filter Text or String in Files

[‘

n

When we run certain commands in Unix/Linux to read or edit text from a string or file, we most times try to filter output to a given section of interest. This is where using regular expressions comes in handy.

n

Read Also: 10 Useful Linux Chaining Operators with Practical Examples

n

What are Regular Expressions?

n

A regular expression can be defined as a strings that represent several sequence of characters. One of the most important things about regular expressions is that they allow you to filter the output of a command or file, edit a section of a text or configuration file and so on.

n

Features of Regular Expression

n

Regular expressions are made of:

n

    n

  1. Ordinary characters such as space, underscore(_), A-Z, a-z, 0-9.
  2. n

  3. Meta characters that are expanded to ordinary characters, they include:n
      n

    1. (.) it matches any single character except a newline.
    2. n

    3. (*) it matches zero or more existences of the immediate character preceding it.
    4. n

    5. [ character(s) ] it matches any one of the characters specified in character(s), one can also use a hyphen (-) to mean a range of characters such as [a-f], [1-5], and so on.
    6. n

    7. ^ it matches the beginning of a line in a file.
    8. n

    9. $ matches the end of line in a file.
    10. n

    11. \ it is an escape character.
    12. n

    n

  4. n

n

In order to filter text, one has to use a text filtering tool such as awk. You can think of awk as a programming language of its own. But for the scope of this guide to using awk, we shall cover it as a simple command line filtering tool.

n

The general syntax of awk is:

n

# awk 'script' filenamern

n

Where 'script' is a set of commands that are understood by awk and are execute on file, filename.

n

It works by reading a given line in the file, makes a copy of the line and then executes the script on the line. This is repeated on all the lines in the file.

n

The 'script' is in the form '/pattern/ action' where pattern is a regular expression and the action is what awk will do when it finds the given pattern in a line.

n

How to Use Awk Filtering Tool in Linux

n

In the following examples, we shall focus on the meta characters that we discussed above under the features of awk.

n

A simple example of using awk:

n

The example below prints all the lines in the file /etc/hosts since no pattern is given.

n

# awk '//{print}'/etc/hostsrn

n

Awk Prints all Lines in a File
Awk Prints all Lines in a File

n

Use Awk with Pattern:

n

I the example below, a pattern localhost has been given, so awk will match line having localhost in the /etc/hosts file.

n

# awk '/localhost/{print}' /etc/hosts rn

n

Awk Print Given Matching Line in a File
Awk Print Given Matching Line in a File

n

Using Awk with (.) wild card in a Pattern

n

The (.) will match strings containing loc, localhost, localnet in the example below.

n

That is to say * l some_single_character c *.

n

# awk '/l.c/{print}' /etc/hostsrn

n

Use Awk to Print Matching Strings in a File
Use Awk to Print Matching Strings in a File

n

Using Awk with (*) Character in a Pattern

n

It will match strings containing localhost, localnet, lines, capable, as in the example below:

n

# awk '/l*c/{print}' /etc/localhostrn

n

Use Awk to Match Strings in File
Use Awk to Match Strings in File

n

You will also realize that (*) tries to a get you the longest match possible it can detect.

n

Let look at a case that demonstrates this, take the regular expression t*t which means match strings that start with letter t and end with t in the line below:

n

this is tecmint, where you get the best good tutorials, how to's, guides, tecmint. rn

n

You will get the following possibilities when you use the pattern /t*t/:

n

this is trnthis is tecmintrnthis is tecmint, where you get trnthis is tecmint, where you get the best good trnthis is tecmint, where you get the best good tutorials, how trnthis is tecmint, where you get the best good tutorials, how tos, guides, trnthis is tecmint, where you get the best good tutorials, how tos, guides, tecmintrn

n

And (*) in /t*t/ wild card character allows awk to choose the the last option:

n

this is tecmint, where you get the best good tutorials, how to's, guides, tecmintrn

n

Using Awk with set [ character(s) ]

n

Take for example the set [al1], here awk will match all strings containing character a or l or 1 in a line in the file /etc/hosts.

n

# awk '/[al1]/{print}' /etc/hostsrn

n

Use-Awk to Print Matching Character in File
Use-Awk to Print Matching Character in File

n

The next example matches strings starting with either K or k followed by T:

n

# awk '/[Kk]T/{print}' /etc/hosts rn

n

Use Awk to Print Matched String in File
Use Awk to Print Matched String in File

n

Specifying Characters in a Range

n

Understand characters with awk:

n

    n

  1. [0-9] means a single number
  2. n

  3. [a-z] means match a single lower case letter
  4. n

  5. [A-Z] means match a single upper case letter
  6. n

  7. [a-zA-Z] means match a single letter
  8. n

  9. [a-zA-Z 0-9] means match a single letter or number
  10. n

n

Lets look at an example below:

n

# awk '/[0-9]/{print}' /etc/hosts rn

n

Use Awk To Print Matching Numbers in File
Use Awk To Print Matching Numbers in File

n

All the line from the file /etc/hosts contain at least a single number [0-9] in the above example.

n

Use Awk with (^) Meta Character

n

It matches all the lines that start with the pattern provided as in the example below:

n

# awk '/^fe/{print}' /etc/hostsrn# awk '/^ff/{print}' /etc/hostsrn

n

Use Awk to Print All Matching Lines with Pattern
Use Awk to Print All Matching Lines with Pattern

n

Use Awk with ($) Meta Character

n

It matches all the lines that end with the pattern provided:

n

# awk '/ab$/{print}' /etc/hostsrn# awk '/ost$/{print}' /etc/hostsrn# awk '/rs$/{print}' /etc/hostsrn

n

Use Awk to Print Given Pattern String
Use Awk to Print Given Pattern String

n

Use Awk with (\) Escape Character

n

It allows you to take the character following it as a literal that is to say consider it just as it is.

n

In the example below, the first command prints out all line in the file, the second command prints out nothing because I want to match a line that has $25.00, but no escape character is used.

n

The third command is correct since a an escape character has been used to read $ as it is.

n

# awk '//{print}' deals.txtrn# awk '/$25.00/{print}' deals.txtrn# awk '/\$25.00/{print}' deals.txtrn

n

Use Awk with Escape Character
Use Awk with Escape Character

n

Summary

n

That is not all with the awk command line filtering tool, the examples above a the basic operations of awk. In the next parts we shall be advancing on how to use complex features of awk. Thanks for reading through and for any additions or clarifications, post a comment in the comments section.

n

‘]