Grep Cheatsheet From Opensource - Com - Osdc - Cheatsheet-Grep
Grep Cheatsheet From Opensource - Com - Osdc - Cheatsheet-Grep
Basics
grep [pattern] FILE
grep '^[A,E].*o' f.txt Find a string starting with A or E and ending in o
grep -v gnu f.txt Find all lines not containing "gnu" (invert match)
grep -w 'a.*o' f.txt Find whole word matches only, ignoring substrings
Output
-c Print only the number of lines containing a match
Output prefixes
-b Print the byte offset of the match within the input file
-H Print the filename containing a match
-h Do not print the filename containing a match
-n Print the line number of each match
-T Print an initial Tab before matches so that output is neatly aligned
Variants
-G Use basic regex (this is the default)
-E Extended regex
-F Interpret the search pattern as a fixed string, not regex
-P Use Perl regex (PCRE)
Regular expression
. Any single character
? Match preceding item zero or one time
* Match preceding item zero or more times
+ Match preceding item one or more times
{2} Match preceding item two times
{3,} Match preceding item three or more times
{,4} Match preceding item at most four times
{1,5} Match preceding item at least once, but no more than five times
[A,B] Match A or B [:alnum:] Alphanumeric character
[3-9] Match all digits 3 to 9 [:alpha:] Alphabetic character
^ Start of a line [:digit:] Digits 0 through 9
$ End of a line [:punct:] Punctuation
\s Space [:space:] Space