Skip to main content
Engineering LibreTexts

05-D.7.4: Handling Text Files - grep Command

  • Page ID
    32517
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The grep Command

    grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.

    Regular expression provides an ability to match a “string of text” in a very flexible and concise manner. A “string of text” can be further defined as a single character, word, sentence or particular pattern of characters.

    Like the shell’s wild–cards which match similar filenames with a single expression, grep uses an expression of a different sort to match a group of similar patterns.

    • [ ]: Matches any one of a set characters
    • [ ] with hyphen: Matches any one of a range characters
    • ^: The pattern following it must occur at the beginning of each line
    • ^ with [ ] : The pattern must not contain any character in the set specified
    • $: The pattern preceding it must occur at the end of each line
    • . (dot): Matches any one character
    • \ (backslash): Ignores the special meaning of the character following it
    • *: Zero or more occurrences of the previous character
    • (dot).*: Nothing or any numbers of characters

    Syntax:

    grep [OPTION...] PATTERNS [FILE...]
    grep [OPTION...] -e PATTERNS ... [FILE...]
    grep [OPTION...] -f PATTERN_FILE ... [FILE...]

    Command Options:

    Options Option Meaning
    -E, --extended-regexp Interpret PATTERNS as extended regular expressions (EREs, see below).
    -F, --fixed-strings Interpret PATTERNS as fixed strings, not regular expressions.
    -c, --count Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines.
    -l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.
    -o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
    -v, --invert-match Invert the sense of matching, to select non-matching lines.
    -f FILE, --file=FILE Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (--regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing.

    The grep command has numerous options which makes it a powerful tool for processing text files.

    pbmac@pbmac-server $ grep  “New[abc]”  filename
    

    The command states look for the text "New," followed by any ONE of the characters in the brackets. So this command specifies the search pattern as any one of the following:

    Newa , Newb or Newc
    

    Another example is:

    pbmac@pbmac-server $ grep  “New[a-e]” filename

    Specifying a dash means "any character in that sequence." So, the above command will match on any of these:

    Newa , Newb, Newc , Newd or Newe
    

    It is okay to use NO options and just search for the word "failed" (or any other word) in a particular file.

    pbmac@pbmac-server $ grep failed /var/log/syslog | head
    Aug  4 00:06:34 pbmac-server colord[1061]: failed to get session [pid 23278]: No data available
    Aug  4 00:06:34 pbmac-server colord[1061]: message repeated 5 times: [ failed to get session [pid 23278]: No data available]
    Aug  4 06:05:14 pbmac-server google-chrome.desktop[2996]: [3035:3040:0804/060514.432826:ERROR:ssl_client_socket_impl.cc(941)] 
    handshake failed; returned -1, SSL error code 1, net_error -113
    Aug  4 06:05:14 pbmac-server google-chrome.desktop[2996]: [3035:3040:0804/060514.474847:ERROR:ssl_client_socket_impl.cc(941)] 
    handshake failed; returned -1, SSL error code 1, net_error -113
    

    As you get more comfortable with regular expressions many commands will become more useful and more powerful.

    Adapted from:
    "Regular Expression in grep" by Akshay Rajput., Geeks for Geeks is licensed under BY-SA 4.0


    05-D.7.4: Handling Text Files - grep Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?