14 Practical examples of the grep command

grep is a command-line utility that allows searching of words and/or patterns in a file or group of files. Its name comes from the ed command g/re/p (globally search a regular expression and print). Grep does a global search by keyword or regular expression and print all matching lines on the standard output.

General syntax

$ grep [OPTIONS] PATTERN [FILE...]
$ grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

In the second form you can specify more than one pattern using several times the -e switch or reading the pattern from a file (-f)

Basic options:

  • -c: Count the number of matches.
  • -E: Interpret PATTERN as an extended regular expression.
  • -f: Gets the patterns from a file, one per line.
  • -i: Case insensitive search.
  • -l: Print the name of each input file where matches are found.
  • -n: Prefix the line number where matches are found.
  • -o: Prints only the part that matches.
  • -v: Invert the sense of matching, to select non-matching lines.

GNU extensions:

  • --color: highlight the word that matches with the color specified in the variable of environment GREP_COLOR, red by default
  • -r,-R: Read all files under each directory, recursively

Full reading at LibreByte

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center