Finding text strings within files (grep command)

Use the grep command to search the specified file for the pattern specified by the Pattern parameter and writes each matching line to standard output.

The following are examples of how to use the grep command:
  • To search in a file named pgm.s for a pattern that contains some of the pattern-matching characters *, ^, ?, [, ], \(, \), \{, and \}, in this case, lines starting with any lowercase or uppercase letter, type the following:
    grep "^[a-zA-Z]" pgm.s
    This displays all lines in the pgm.s file that begin with a letter.
  • To display all lines in a file named sort.c that do not match a particular pattern, type the following:
    grep -v bubble sort.c
    This displays all lines that do not contain the word bubble in the sort.c file.
  • To display lines in the output of the ls command that match the string staff, type the following:
    ls -l | grep staff