Grep : The power tool

The grep command is a very powerful way to find documents on your computer or to see if a file contains a word or use one of many forms of regular expression to search for a pattern instead. Grep can check the file that you specify or can search an entire tree of your file system recursively looking for matching files.
Grep (Global Regular Expression Print) is installed by default on almost every distribution of Linux, BSD and UNIX, and is even available for Windows. GNU and the Free Software Foundation distribute Grep as part of their suite of open source tools.
So, why Grep? answer is simple, because it is fast.
GNU grep is fast because it AVOIDS LOOKING AT EVERY INPUT BYTE, it EXECUTES VERY FEW INSTRUCTIONS FOR EACH BYTE that it does look at. GNU grep uses the well-known Boyer-Moore algorithm, which looks first for the final letter of the target string, and uses a lookup table to tell it how far ahead it can skip in the input whenever it finds a non-matching character.
More information from the author of grep : Mike Haertel.
Grep can be majorly classified into:
1. fgrep : Fixed String grep search or also denoted by grep – F. It uses only fixed string searches i.e. cannot have any regular expressions.
2. egrep : Extended Regular Expressions grep search or also denoted by grep – E. Advanced searching based on patterns.
Usually, regular expressions are included in the Grep command in the following format:
grep [options] [regexp] [filename]

Usage
Consider a file GrepTest and we shall be operating the usage of grep on this file.
For our demo purposes, we use the following file content.

ssaxena36@root:~$ cat GrepTest
I ate apple.
I want a dog.
I like linux.
I want some banana.
A free supply of random text.
The RANDOM text resource in website.
Generate random text by randomizing sentences.
ssaxena36@root:~$

1. Display fixed pattern

ssaxena36@root:~$ grep 'have' GrepTest
I have a cat.
I have a dog.
ssaxena36@root:~$ 

OR

ssaxena36@root:~$ cat GrepTest | grep 'have'
I have a cat.
I have a dog.
ssaxena36@root:~$ 

2. Whole-word search:
It considers given pattern as a whole word using grep -w [pattern/string]

ssaxena36@root:~$ cat GrepTest | grep -w 'random'
A free supply of random text.
Generate random text by randomizing sentences.
ssaxena36@root:~$

3. Searching through sub-folders (Recursive Mode):
Returns found pattern in every file of the given directory.

ssaxena36@root:~/Desktop/Java$ grep -R 'class' 
fio2.txt:class FastIO2 {
fio2.txt:	private static class InputReader {
fio2.txt:	private static class OutputWriter {
fio.txt:class MIXTURES {
ssaxena36@root:~/Desktop/Java$

4. Case Insensitive Search:
Simple grep -i searching file without case matching

ssaxena36@root:~$ cat GrepTest | grep -i RANDOM
A free supply of random text.
The RANDOM text resource in website.
Generate random text by randomizing sentences.
ssaxena36@root:~$ 

5. Print except the match:
grep -v [pattern/string] prints the lines that do not have the given matching possibility.

ssaxena36@root:~$ cat GrepTest | grep -v I
A free supply of random text.
The RANDOM text resource in website.
Generate random text by randomizing sentences.
ssaxena36@root:~$ 

6. Print lines after the match:
grep -AX [pattern] where X defines number of lines next to matched line to be printed.

ssaxena36@root:~$ cat GrepTest | grep -A2 'banana'
I want some banana.
A free supply of random text.
The RANDOM text resource in website.
ssaxena36@root:~$

7. Print lines before the match:
grep -BX [pattern] where X defines number of lines before the matched line to be printed.

ssaxena36@root:~$ cat GrepTest | grep -B2 'banana'
I want a dog.
I like linux.
I want some banana.
ssaxena36@root:~$

8. Print lines before as well as after the match:
grep -CX [pattern] where X defines number of lines before and after the matched line to be printed.

ssaxena36@root:~$ cat GrepTest | grep -C1 'banana'
I like linux.
I want some banana.
A free supply of random text.
ssaxena36@root:~$

Using Regular Expressions:
Regular Expressions that can be used in grep can be of following types and formats:
   1.   . (single dot) refers any single character.
   2.   ? means that the preceding item is optional, and if found, will be matched at the most, once.
   3.   * means that the preceding item will be matched zero or more times.
   4.   + means the preceding item will be matched one or more times.
   5.   {n} means the preceding item is matched exactly n times, while   {n,} means the item is matched n or more times.
{n,m}
means that the preceding item is matched at least n times, but not more than m times.   {,m} means that the preceding item is matched, at the most, m times.
   6. The caret  ^ and the dollar sign  $ are meta-characters that respectively match the empty string at the beginning and end of a line. They are termed anchors, since they force the match to be “anchored” to beginning or end of a line, respectively.
   7. A bracket expression is a list of characters enclosed by  [ and  ]. For example, the regular expression ‘[0123456789]’ or ‘[0-9]’ (in range) matches any single digit.
   8. The character class tool is more flexible.
   ‘[:alnum:]’ Alphanumeric characters: ‘[:alpha:]’ and ‘[:digit:]’, same as ‘[0-9A-Za-z]’.
   ‘[:alpha:]’ : Alphabetic characters: same as ‘[A-Za-z]’.
   ‘[:blank:]’ : Blank characters: space and tab.
   ‘[:digit:]’ : Digits: 0 – 9.
   ‘[:lower:]’ : Lower-case letters.
   ‘[:print:]’ : Printable characters:  ‘[:alnum:]’, ‘[:punct:]’ , and space.
   ‘[:punct:]’ : Punctuation characters.
   ‘[:space:]’ : Space characters, this is tab, newline, vertical tab.
   ‘[:upper:]’ : Upper-case letters.
More at : GNU Grep 3.0
Examples on Regular Expression and egrep:
1. List of all Directories: All directories have a ‘d’ before list display detail so:

ssaxena36@root:~$ ls -la | egrep ^d
drwxr-xr-x 33 ssaxena36 ssaxena36 4096 Aug 27 22:40 .
drwxr-xr-x  3 root      root      4096 Aug 26 22:58 ..
drwx------  3 ssaxena36 ssaxena36 4096 Aug 27 05:16 .adobe
drwxrwxr-x  3 ssaxena36 ssaxena36 4096 Aug 27 22:30 .android
ssaxena36@root:~$

2. Lines matched but their line number as well:

ssaxena36@root:~$ cat GrepTest | egrep -n random
5:A free supply of random text.
7:Generate random text by randomizing sentences.
ssaxena36@root:~$

3. To know how many lines did match:

ssaxena36@root:~$ cat GrepTest | egrep -c random
2
ssaxena36@root:~$

4. Any line with two or more continuous vowels in a row:

ssaxena36@root:~$ cat GrepTest | egrep '[aeiou]{2,}'
A free supply of random text.
The RANDOM text resource in website.
ssaxena36@root:~$ 

5. The last character on the line is a vowel(excluding the ‘.'[full stop]):

ssaxena36@root:~$ cat GrepTest | egrep '[aeiou].$'
I ate apple.
I want some banana.
The RANDOM text resource in website.
ssaxena36@root:~$ 

6. Print If a line contains an article:

ssaxena36@root:~$ cat GrepTest | egrep -w 'a|an|the'
I want a dog.
ssaxena36@root:~$ 

7. Lines not beginning with I:

ssaxena36@root:~$ cat GrepTest | egrep -v ^I
A free supply of random text.
The RANDOM text resource in website.
Generate random text by randomizing sentences.
ssaxena36@root:~$ 

8. Case insensitive word search using Regular Expressions:

ssaxena36@root:~$ cat GrepTest | egrep -w [rR][aA][nN][dD][oO][mM]
A free supply of random text.
The RANDOM text resource in website.
Generate random text by randomizing sentences.
ssaxena36@root:~$ 

9. List of all files/directories which are readable atleast by owner:

ssaxena36@root:~$ ls -la | egrep ^.r
drwxr-xr-x  3 root      root      4096 Aug 26 22:58 ..
drwx------  3 ssaxena36 ssaxena36 4096 Aug 27 05:16 .adobe
drwxrwxr-x  3 ssaxena36 ssaxena36 4096 Aug 27 22:30 .android
ssaxena36@root:~$

10. List of all files/directories which have full execution permission(rwx) by at least one of owner/group/other:

ssaxena36@root:~$ ls -la | egrep '[r][w][x]{1,}'
drwx------  3 ssaxena36 ssaxena36 4096 Aug 27 05:16 .adobe
drwxrwxr-x  3 ssaxena36 ssaxena36 4096 Aug 27 22:30 .android
drwxrwxr-x  3 ssaxena36 ssaxena36 4096 Aug 26 19:11 AndroidStudioProjects
drwx------ 18 ssaxena36 ssaxena36 4096 Aug 27 12:53 .cache
ssaxena36@root:~$

Try more of them, incase you have some more query comment down below.

References:
1. Grep in Depth
2. Open Source For U
3. GNU Grep 3.0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s