srdotlibre avatar

14 Practical examples of the grep command

srdotlibre

Published: 20 Jul 2020 › Updated: 20 Jul 202014 Practical examples of the grep command

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

Leave 14 Practical examples of the grep command to:

Written by

Read more #grep posts


Best Posts From srdotlibre

We have not curated any of srdotlibre's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From srdotlibre