Learning AWK Programming
上QQ阅读APP看书,第一时间看更新

Dot

The "." character is one of those special metacharacters that matches any character, except the end-of-line character. In the following example, we match a sequence of three characters that begin with C and end with T, as follows:

$ echo -e "C1T\nCaT\nC@T\ncAT" | awk '/C.T/ { print }'

The output on execution of the preceding program is as follows:

C1T
CaT
C@T

Let's create one file for practicing dot regular expressions:

$ vi dot_regex.txt 
Lets go for a walk
Singing is good hobby
We will talk on this matter
Ping me when you are free
(that is cool)
My son birthday is on 24/04/14
I will be going to Singapore on 24-04-14
(this)

Now, let's execute some regular expressions on the dot_regex.txt file:

  1. Match all strings with any character preceded by 'ing', as follows:
$ awk '/.ing/{ print }'  dot_regex.txt

The output on execution of this code is:

Singing is good hobby
Ping me when you are free
I will be going to Singapore on 24-04-14
  1. Match all strings that contain a space, followed by a character, and further followed by 'alk':
$ awk '/ .alk/{ print }'  dot_regex.txt

The output on execution of the preceding code is as follows:

Lets go for a walk
We will talk on this matter
  1. Match the date with any separator, as follows:
$ awk '/24.04.14/{ print }' dot_regex.txt

The output on execution of the given code is as follows:

My son birthday is on 24/04/14
I will be going to Singapore on 24-04-14

A summary of dot/period is as follows:

Pattern

Matches

.

Matches any single character anywhere

.C

Matches any single character followed by "C" anywhere on the line

A.C

Matches a sequence of three characters that begin with A and end with C anywhere on the line

^.$

Matches any string containing exactly one character

...

Matches any three consecutive characters anywhere on the line

\.$

Matches a period at the end of the line