GNU/Linux Command – GREP

grep — (Global Regular Expression Print) Print lines matching a pattern

Summary:
`grep’ searches the input files for lines containing a match to a given pattern list. Grep has a no .of useful options, character classes and support regular expressions.

Examples:
$ grep -i bharathi Midas.txt -- Print the lines which contains the string "bharathi" in the Midas.txt file. $ grep -v bharathi ILUGC.txt -- Print the line which is not thestring "bharathi". $ grep -A 2 "ram" file -- Along with matching line, prints the next2 lines. $ grep -B 2 "siva" file -- Along with matching line, prints theprevious 2 lines. $ grep -C 2 "rupa" file -- Along with matching line, prints the previous and next 2 lines. $ grep -n "vijay" file -- Print the matching lines with itsline number. $ grep -c "Linux" file -- Prints count of match. $ grep -w "is" file -- Match the whole word only. Match "is",but not "this". $ grep "^A" file -- Prints the line starting with A. $ grep "Z$" file -- Prints the line with ending letter Z. $ grep '^[012]' file -- Prints the line starting with 0/1/2. $ grep -E "Linux|Unix" file -- Print the lines containing "Linux"or "Unix".
Read : info grep / egrep / fgrep

Advertisements
Tagged with: ,
Posted in Uncategorized

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 )

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

%d bloggers like this: