Awk Regular Expression Commands and Examples

Syntax

Regular expression for awk can be used with the following syntax.

awk ‘/REGULAR_EXPRESSION/’

  • REGULAR_EXPRESSION is the regular expression we want to run . The regular expression is surrounded with / and /

Example Data

We will use follow file named data.txt for example scripts.

ismail 33 male                                                                                                        ahmet 5 male

elif 8 female                                                                                                         

Any Single Character

The first and simplest example with regular expression is expressing single character in regular expression. In this we will filter words starts with is and ends with ail but contain single character between then where character is not important.

$ awk ‘/is.ail/’ data.txt

Any Single Character

Any Single Character

Start Of Line

We may need to specify the start of the line. We will use ^ sig to specify start of the line. In this example we will look the lines starts with a .

$ awk ‘/^a/’ data.txt

Start Of Line

Start Of Line

End Of Line

Like previous example we can also specify the end of the line with the $ sign. In this example we will look lines those ends with le .

$ awk ‘/le$/’ data.txt

End Of Line

End Of Line

Match Character Set

We can specify multiple characters to hope match one of them. We will put those characters into [...] . In this example we will match lines those have sm or hm

$ awk ‘/[sh]m/’ data.txt

Match Character Set

Match Character Set

Exclusive Set

Exclusive set is the reverse operation of character set. The provided set matched lines will be removed from output. We will express exclusive set as [^...] . In this example we want to list lines those do not have sm or hm .

$ awk ‘/[^sh]m/’ data.txt

OR or Multiple Words

We can specify multiple words for a single search operation. This will match all lines those have one of the provided words. In this example we will match lines ahmet or ismail

$ awk “/ismail|ahmet/” data.txt

OR or Multiple Words

OR or Multiple Words

Match Zero or One Occurrence

We can specify to expect given character zero or one occurrence. We will use ? sign. In the example we will look i character zero or one occurrence.

$ awk “/l?i/” data.txt

Match Zero or One Occurrence

Match Zero or One Occurrence

Match Zero or More Occurrence

We can specify zero or more multiple occurrence with * sign. In this example we will look for word male , malee , maleee , …

$ awk “/male*/” data.txt

Match Zero or More Occurrence

Match Zero or More Occurrence

Match One or More Occurrence

We can also expect single or more occurrence for given character or word. We will use + for this. In this example we will look male , mmale , mmmale .

$ awk “/m*ale/” data.txt

Match One or More Occurrence

Match One or More Occurrence

Match Groups

Up to now we have generally worked with single characters but in real world we may need multiple characters or words to match. We will use (...) . In this example we will look one or more occurrence of male

$ awk “/(male)+/” data.txt

Match Groups

Match Groups

Comments