grep

linux

https://www.cyberciti.biz/faq/grep-regular-expressions/
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_02.html - done reading
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/
http://www.folkstalk.com/2012/01/grep-command-in-unix-examples.html
http://www.computerhope.com/unix/ugrep.htm
http://www.thegeekstuff.com/2011/10/grep-or-and-not-operators/
http://www.linuxtechi.com/linux-grep-command-with-14-different-examples/
http://www.techonthenet.com/unix/basic/grep.php
http://www.panix.com/~elflord/unix/grep.html
http://www.tecmint.com/12-practical-examples-of-linux-grep-command/
http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
http://alvinalexander.com/unix/edu/examples/grep.shtml
http://www.thegeekstuff.com/2011/01/regular-expressions-in-grep-command/
http://www.thegeekstuff.com/2011/01/advanced-regular-expressions-in-grep-command-with-10-examples-%e2%80%93-part-ii/
http://www.thegeekstuff.com/2010/10/bzcommand-examples/
http://www.thegeekstuff.com/2009/05/zcat-zless-zgrep-zdiff-zcmp-zmore-gzip-file-operations-on-the-compressed-files/

grep [options] "regex" files
  1. -i: case insensative match
  2. -v: inverted match (print all lines that do not match). Select non-matching lines.
  3. -c: count the number of match for each file. Suppress the normal output, and print the count of matching lines for each input files
  4. -f FILE, —file=FILE: Obtain patterns from FILE, one per line. An empty file contains zero patterns, and therefore match nothing
  5. -H, —with-filename: Print the file name for each match
  6. -h, —no-filename: Suppress the prefixing of filename on output when multiple files are searched.
  7. -l: (letter l) print only the name of the file that match. Suppress normal output. Instead, print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.
  8. -L, —file-without-match: opposite of lowercase letter L. Suppress the normal output. Instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.
  9. -n: display line number. Prefix each line of output with the line number within its input file
  10. -o: (letter o) show only part that match (handy if you have line that span several screens)
  11. -w word: if you are searching for a word and want to suppress partial matches
  12. -r: recursive. Read all files under each directory, recursively.

How can we use Perl-compatible regular expression with grep?

echo 'This is test number 5' | grep -P '[[:digit:]]'

The above example print the line again because that is what grep do when it find a match. The number 5 matches with the POSIX [[:digit:]]. If we want to see the part that match, add the -o option to the grep command. The -o option show only part that match.

What is the purpose of the grep command?

grep search the named input files (or standard input if no files are given, or the filename - is given) for lines matching a given pattern. By default, grep print the matching lines.

How can we search for a given string or pattern in a file?

grep -i "pattern" fileName

How can we search for a given string or pattern in all files within a given directory?

grep -r "pattern" *

How can we read the manual page for grep?

man grep

How can we print the matched line, along with the 3 lines after it?

grep -A 3 -i "pattern" *

How can we display the line number of the line that contains the pattern?

Use the -n option:

grep -n "pattern" *

How can we invert the match?

Use the -v option, which inverts the match. It only print all lines that do not match the given pattern.

grep -v "pattern" *

How can we output the number of matched lines?

Use the -c option. It suppresses the normal output, and print a count of matching lines for each input file. With the -v, —invert-match option, this options counts non-matching lines.

How can we output the number of non-matching lines?

Use the -c option with the -v option:

grep -vc "pattern" *

How can we do a case-insensitive search?

Use the -i option.

What is the requirement for using regular expression with grep?

We use GNU grep in these examples, which supports extended regular expressions. GNU grep is the default on Linux systems. If you are working on proprietary systems, check with the -V option which version you are using. GNU grep can be downloaded from http://gnu.org/directory/. To determine if you are using GNU grep:

grep -V

How can we search for lines that start with a given pattern?

Prefix the pattern with the ^:

grep ^pattern *

If this does not work, check to see if your particular version of grep has support for extended regular expression built-in.

How can we search for lines that end with a given pattern?

Postfix the pattern with the $:

grep pattern$ *

If this does not work, check to see if your particular version of grep has support for extended regular expression built-in.

How can we use character classes with grep?

Character classes (bracket expressions) are lists of characters enclosed by "[" and "]". It matches any single character in that list; if the first character of the list is the caret, "^", then it matches any character NOT in the list. For example, the regular expression "[0123456789]" matches any single digit.

Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale's collating sequence and character set. For example, in the default C locale, "[a-d]" is equivalent to "[abcd]". Many locales sort characters in dictionary order, and in these locales "[a-d]" is typically not equivalent to "[abcd]"; it might be equivalent to "[aBbCcDd]", for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value "C".

Finally, certain named classes of characters are predefined within bracket expressions. See the grep man or info pages for more information about these predefined expressions.

grep [yf] *

The above command search all files within the current directory for lines containing the y or f character.

How can we search for lines containing the literal dot (.) character?

Use the "." to match any single character. If you want to display lines containing the literal dot character, use the -F option to grep.

How can we use grep with extended regular expression support?

Escape character: \

\.? match one or more .  \(eric\)\? will make the group optional
Group: \(..\)
Matching anything between: [...]
[[:alpha:]] is the same as [a-zA-Z]
[[:upper:]] is the same as [A-Z]
[[:digit:]] is the same as [0-9]
[[:lower:]] is the same as [a-z]
[[:alnum:]] is the same as [0-9a-zA-Z]
[[:space:]] match any white space including tabs

The caret ^ inside [] is the same for that in Perl.  It is used to negate.

\{n\}: match exactly n times
\*: match zero or more times
\+: match one or more times
\{n,\}: match n or more times
\{n,m\}: match at least n times but at most m times
^: start of line
$: end of line
\|: alternation
  1. Back reference: \n
  2. The following characters are special and need to be escaped: ? \ . [ ] ^ $
  3. The $ loses it meaning if characters follow it, and the ^ loses its meaning if other characters precede it.
  4. A closing square bracket lose its special meaning if placed first in a list.
  5. The dash - loses its usual meaning inside lists if it is placed last
  6. A caret ^ loses its special meaning if it is not placed first.
  7. Most special characters lose their meaning inside square brackets.
grep -E (extended grep) is more straight forward like Perl.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License