The Grep RegEx Command

  • Html
  • Pdf
  • video

Grep RegEx. In grep with regex you can do praticaly an infinite number of different search, regex is a language in itself , it’s a language to identify your search criteria, what you want to find and what result you want.

With the following ls –ltr command result:

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads
drwxrwxr-- 5 morins morins 4096 Oct 7 16:40 kdenlive
drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos
-rw-rw-r-- 1 morins morins 337597 Oct 7 16:53 out.ogv
drwxrwxr-- 2 morins morins 4096 Oct 7 20:19 Documents
-rw-r--r-- 1 morins morins 447 Dec 8 19:30 color.txt
-rwxrwxr-- 1 morins morins 408 Dec 8 19:30 color.sh
-rwxrwxr-- 1 morins morins 624 Dec 8 19:30 color4.sh
-rwxrwxr-- 1 morins morins 623 Dec 8 19:30 color3.sh
-rwxrwxr-- 1 morins morins 1126 Dec 8 19:30 color2.sh
-rwxrwxr-- 1 morins morins 3076 Dec 8 19:30 test2
-rwxrwxr-- 1 morins morins 662 Dec 8 19:30 test
-rwxrwxr-- 1 morins morins 72 Dec 8 19:30 scripts.sh
-rwxrwxr-- 1 morins morins 68 Dec 8 19:30 quotes.sh
-rw-r--r-- 1 morins morins 88 Dec 8 19:30 name
-rwxrwxr-- 1 morins morins 134 Dec 8 19:30 format.sh
-rw-r--r-- 1 morins morins 8980 Dec 8 19:30 examples.desktop
-rw-r--r-- 1 morins morins 229 Dec 8 19:30 test.txt
-rw-r--r-- 1 morins morins 229 Dec 8 19:30 test3.txt
-rwxrwxr-- 1 morins morins 4373 Dec 8 19:30 test3
-rw-r--r-- 1 morins morins 229 Dec 8 19:30 test2.txt
-rwx------ 1 morins morins 113 Dec 8 19:55 t
-rwx------ 1 morins morins 228 Dec 30 22:57 test_zenity.sh

If you do :

1

ls –ltr | grep d

you will get :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads
drwxrwxr-- 5 morins morins 4096 Oct 7 16:40 kdenlive
drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos
drwxrwxr-- 2 morins morins 4096 Oct 7 20:19 Documents
-rw-r--r-- 1 morins morins 8980 Dec 8 19:30 examples.desktop

Ok you get all the folder , which is what you want , but you also get the file examples.desktop which you don’t want.

So instead you will do :
2

ls –ltr | grep “^d”

you get :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads
drwxrwxr-- 5 morins morins 4096 Oct 7 16:40 kdenlive
drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos
drwxrwxr-- 2 morins morins 4096 Oct 7 20:19 Documents

the (carret) ^ indicate the beginning of the file , so it means every lines that start with letter ‘d’

Now if you want let say to have all the file that end with an ‘s’.

You will do the folowing command :
3

ls –ltr | grep “s$”

and you get :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures

drwxrwxr-- 6 morins morins 4096 Oct 12 2015 Downloads

drwxrwxr-- 2 morins morins 4096 Oct 25 2015 Videos

drwxrwxr-x 2 morins morins 4096 Nov 1 2015 scripts

drwxrwxr-- 2 morins morins 4096 Mar 6 21:11 Documents

it means all lines that contain ‘s’ immediately folowed by the end of the line.

Now let say you want to select all lines with a folder of month feb.

You could do :
4

ls –ltr | grep “^d.*Feb”

you obtain :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads

That means , give me the lines that start with a ‘d’ , have any charactere many time, folow by ‘Feb’ , don’t forget the ‘F’ in caps is essential , because linux is case sensitive.

Here are some basic symbol definition :
Symbol
Definition

^

Beginning of the line

$

End of line

.

Any charactere (dot)

*

Many time the preceding symbol or charactere

[]

Selection of possible charactere

You can also do:

5

ls –ltr | grep “[FND]eb”

the result is :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads

Like that you will get any lines that contain either Feb, Neb or Deb , but as in our list only the folder correspond to that and there is no lines with either Neb or Deb we got the same lines as per the preceding example.

For example if you do :

6

ls –ltr | grep “[PV]i”

you get :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures

drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos

Because it’s the only lines that contain either ‘Pi’ or ‘Vi”.

Please feel free to leave comments, suggestion or to share this post!