Table 4.3. Review of grep
Command | What It Does |
---|
grep '\<Tom\>' file | Prints lines containing the word Tom. |
grep 'Tom Savage' file | Prints lines containing Tom Savage. |
grep '^Tommy' file | Prints lines if Tommy is at the beginning of the line. |
grep '\.bak$' file | Prints lines ending in .bak. Single quotes protect the dollar sign ($) from interpretation. |
grep '[Pp]yramid' * | Prints lines from all files containing pyramid or Pyramid in the current working directory. |
grep '[A–Z]' file | Prints lines containing at least one capital letter. |
grep '[0–9]' file | Prints lines containing at least one number. |
grep '[A–Z]...[0–9]' file | Prints lines containing five-character patterns starting with a capital letter and ending with a number. |
grep –w '[tT]est' files | Prints lines with the word Test and/or test. |
grep –s 'Mark Todd' file | Finds lines containing Mark Todd, but does not print the lines. Can be used when checking grep's exit status. |
grep –v 'Mary' file | Prints all lines not containing Mary. |
grep –i 'sam' file | Prints all lines containing sam, regardless of case (e.g., SAM, sam, SaM, sAm). |
grep –l 'Dear Boss' * | Lists all filenames containing Dear Boss. |
grep –n 'Tom' file | Precedes matching lines with line numbers. |
grep "$name" file | Expands the value of variable name and prints lines containing that value. Must use double quotes. |
grep '$5' file | Prints lines containing literal $5. Must use single quotes. |
ps –ef| grep '^ *user1' | Pipes output of ps –ef to grep, searching for user1 at the beginning of a line, even if it is preceded by zero or more spaces. |