Hack #8-> Regular Expression in Grep

Regular expressions are used to search and manipulate the text, based

on the patterns. Most of the Linux commands and programming

languages use regular expression.

This hack explains how to use most frequently used reg-ex operators in

Grep command.

Beginning of line ( ^ )

In grep command, caret Symbol ^ matches the expression at the start of

a line. In the following example, it displays all the line which starts with

the Nov 10. i.e All the messages logged on November 10.

$ grep “^Nov 10” messages.1

Nov 10 01:12:55 gs123 ntpd[2241]: time reset +0.177479 s

Nov 10 01:17:17 gs123 ntpd[2241]: synchronized to

LOCAL(0), stratum 10

Nov 10 01:18:49 gs123 ntpd[2241]: synchronized to

15.1.13.13, stratum 3

Nov 10 13:21:26 gs123 ntpd[2241]: time reset +0.146664 s

Nov 10 13:25:46 gs123 ntpd[2241]: synchronized to

LOCAL(0), stratum 10

Nov 10 13:26:27 gs123 ntpd[2241]: synchronized to

15.1.13.13, stratum 3

The ^ matches the expression in the beginning of a line, only if it is the

first character in a regular expression. ^N matches line beginning with

N.

End of the line ( $)

Character $ matches the expression at the end of a line. The following

command will help you to get all the lines which ends with the word

“terminating”.

$ grep “terminating.$” messages

Jul 12 17:01:09 cloneme kernel: Kernel log daemon

terminating.

Oct 28 06:29:54 cloneme kernel: Kernel log daemon

terminating.

From the above output you can come to know when all the kernel log

has got terminated. Just like ^ matches the beginning of the line only if

it is the first character, $ matches the end of the line only if it is the last

character in a regular expression.

Count of empty lines ( ^$ )

Using ^ and $ character you can find out the empty lines available in a

file. “^$” specifies empty line.

$ grep -c “^$” messages anaconda.log

messages:0

anaconda.log:3

The above commands displays the count of the empty lines available in

the messages and anaconda.log files.

Single Character (.)

The special meta-character “.” (dot) matches any character except the

end of the line character. Let us take the input file which has the content

as follows.

$ cat input

  1. first line
  2. hi hello
  3. hi zello how are you
  4. cello
  5. aello
  6. eello
  7. last line

Now let us search for a word which has any single character followed by

ello. i.e hello, cello etc.,

$ grep “.ello” input

  1. hi hello
  2. hi zello how are you
  3. cello
  4. aello
  5. eello

In case if you want to search for a word which has only 4 character you

can give grep -w “….” where single dot represents any single character.

Zero or more occurrence (*)

The special character “*” matches zero or more occurrence of the

previous character. For example, the pattern ’1*’ matches zero or more

’1′.

The following example searches for a pattern “kernel: *” i.e kernel: and

zero or more occurrence of space character.

$ grep “kernel: *.” *

messages.4:Jul 12 17:01:02 cloneme kernel: ACPI: PCI

interrupt for device 0000:00:11.0 disabled

messages.4:Oct 28 06:29:49 cloneme kernel: ACPI: PM-Timer

IO Port: 0x1008

messages.4:Oct 28 06:31:06 btovm871 kernel: sda: sda1

sda2 sda3

messages.4:Oct 28 06:31:06 btovm871 kernel: sd 0:0:0:0:

Attached scsi disk sda

.

.

In the above example it matches for kernel and colon symbol followed

by any number of spaces/no space and “.” matches any single

character.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s