Awk tutorial: awk regex example

Linux awk command has great advantages in text processing, which contains regular expression processing.

Usually I use the linux grep command to filter and filter text.

Today, I will introduce to you how to use the awk regular command for text filtering and processing.

Before the formal introduction, first spread a few concepts to everyone:

What Is Regex?

Usually, regular expressions or regex are the text patterns you define.

Types of Regex

Linux has two regular expression engines:

  • BRE: The Basic Regular Expression engine.
  • ERE: The Extended Regular Expression engine.

Compared to the BRE engine, the ERE engine comes with some programming languages ​​that provides more patterns.

Awk using BRE regex Patterns

➜  awk '/linux-tutorial/{print $0}' ../log/www.linuxcommands.site_nginx.log | head -3

Special Characters

.*[]^${}\+?|()

Sometimes you will encounter situations where special characters are handled, for example:

➜ awk -F' ' '{print $11}' ../log/www.linuxcommands.site_nginx.log | awk '/\?/{print $0}' | head -5
You need to escape these special characters using the backslash character (\).

Awk using ERE regex Patterns

➜ awk -F' ' '{print $7}' ../log/www.linuxcommands.site_nginx.log | awk '/amp\/$/{print $0}' | head -5

The caret character (^) matches the start of text:

➜ awk -F' ' '{print $7}' ../log/www.linuxcommands.site_nginx.log | awk '/^\/robot/{print $0}' | head -5

The character class is defined using square brackets [] like this:

➜ awk '/[h|e]re/{print $0}' test-3.txt

Ranges

➜ awk -F' ' '{print $7}' ../log/www.linuxcommands.site_nginx.log | awk '/[0-9]$/{print $0}' | head -5


Related post

awk command tutorial in linux/unix with examples and use cases
Awk tutorial: awk merge files base on the keyword
AWK tutorial: awk custom function and awk example
Awk tutorial: find and kill process use awk
Awk tutorial: awk -F and awk BEGIN{ FS … }
Awk tutorial: awk group by count
Awk tutorial: awk loop example: awk for and while
awk if else, if else if else and awk if else nested example
Awk tutorial: awk NR FNR difference
Awk tutorial: awk sort uniq
Awk tutorial: split on character awk or cut
Awk tutorial: awk newline character
Awk tutorial: three ways of awk execution
How to print tree directory structure in linux/unix
Linux shell batch move files, delete spaces, rename files

Add a Comment

Your email address will not be published. Required fields are marked *