Thursday, January 18, 2018

Linux grep Command

Have you ever been confronted with the task of looking for a particular string or pattern in a file, yet have no idea where to start looking? Well then, here is grep to the rescue!

grep is a powerful file pattern searcher that comes equipped on every distribution of Linux. If, for whatever reason, it is not installed on your system, you can easily install it via your package manager (apt get on Debian/Ubuntu and yum on RHEL/CentOS/Fedora).
 
$ sudo apt-get install grep         #Debian/Ubuntu
$ sudo yum install grep             #RHEL/CentOS/Fedora

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. 
By default, grep prints the matching lines. In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. 
fgrep is the same as grep -F. rgrep is the same as grep -r. 
Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified. 
The grep has no limits on input line length other than available memory, and it can match arbitrary characters within a line. 
If the final byte of an input file is not a newline, grep silently supplies one. 
Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text.

Using grep for search files:
To search /etc/passwd file for the user harry, enter the following command.
$ grep manish/etc/passwd

Sample outputs:
manish:x:1000:1000:manish,,,:/home/manish:/bin/ksh

You can force grep to ignore word case i.e match manish, MANISH, Manish and all other combination with the -i option:
$ grep -i “manish” /etc/passwd

If you want to search for a word, and avoid matching substrings use ‘-w ‘option. Just doing a normal search will show all the lines. 
The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.

$ grep -i “is” samplefile

THIS LETTER IS THE 1ST UPPER CASE LETTER IN THIS LINE.
this letter is the 1st lower case letter in this LINE.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.

The following example is the WORD grep where it is searching only for the word “is”.
 Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.

grep -iw “is” samplefile

THIS LETTER IS THE 1ST UPPER CASE LINE IN THIS FILE.
this letter is the 1st lower case letter in this line.
Two lines above this line is empty.
And this is the last line


No comments:

Post a Comment

Configure NFS Client

This example is based on the environment below. +----------------------+           |           +----------------------+ | [...