grep
100%

Text-Fu · Lesson 16

grep

Learn how to select lines with fixed strings or regular expressions and interpret grep results.

The grep command selects input lines that match a pattern. It can search named files or stdin, print matching context, count selected lines, and communicate whether a match was found through its exit status.

Matching Lines in a File

Pass a pattern followed by one or more input files:

$ grep 'fox' sample.txt

By default, GNU grep interprets the pattern as a basic regular expression and prints every selected line. Quote patterns to keep spaces and shell metacharacters from being interpreted by the shell first.

Use -F when the pattern should be treated as a fixed string rather than a regular expression:

$ grep -F 'price: $5.00' products.txt

Which command searches products.txt for the literal text price: $5.00 without treating pattern characters as regular-expression syntax?

Selecting Pattern Syntax

GNU grep offers three commonly used pattern modes:

  • Default: Basic regular expressions.
  • -E: Extended regular expressions, including operators such as |, +, and ? without backslashes.
  • -F: Fixed strings with no regular-expression operators.

Anchors such as ^ and $ match the beginning and end of a line. To match filenames ending in the literal suffix .txt within a text list:

$ grep -E '\.txt$' filenames.txt

The backslash makes the dot literal; an unescaped . in a regular expression matches any single character.

Which extended regular expression matches lines ending with the literal suffix .txt?

Supplying Patterns Safely

Use -e PATTERN to supply a pattern explicitly. This is particularly useful when the pattern begins with -, because quoting alone does not stop option parsing:

$ grep -e '-v' settings.conf

You can repeat -e to select lines matching any supplied pattern. Use -f patterns.txt to read one pattern per line from a file.

Which command searches settings.conf for the pattern -v rather than interpreting it as an option?

Controlling Selected Output

  • -i: Ignore case distinctions.
  • -n: Prefix selected lines with line numbers.
  • -v: Select lines that do not match.
  • -c: Print the count of selected lines for each input file.
  • -o: Print only each nonempty matching part rather than the full selected line.

For example, count lines containing fox, ignoring case:

$ grep -ic 'fox' sample.txt

-c counts selected lines, not the total number of matches within those lines. A line containing fox fox contributes one to the count. When you specifically need nonoverlapping match occurrences with GNU grep, grep -o PATTERN | wc -l is one possible pipeline.

data.txt has one line containing error error and two lines with no match. What does grep -c 'error' data.txt report?

Filtering stdin and Searching Directories

When no input file is named, grep reads stdin and fits naturally in a pipeline:

$ env | grep '^USER='

Use -r to search readable files recursively below a directory:

$ grep -r 'listen_port' config/

Diagnostics such as permission errors go to stderr and are not matching input. Narrow the search path and understand permissions rather than immediately elevating access.

In generate-report | grep 'failed', what input does grep search?

Interpreting the Exit Status

For ordinary searches, GNU grep returns status 0 when at least one line is selected, 1 when no line is selected, and 2 for an error. This lets scripts test for a match without treating “no match” as the same condition as an unreadable file or invalid pattern.

Options such as -q suppress normal output and stop after a match is found, which is useful for condition checks. Do not infer success from empty display alone: -q, redirection, no match, and an error can all produce little or no stdout, while their statuses differ.

To practice fixed-string and regular-expression searches, try these hands-on labs:

  1. Search Text with grep in Linux - Practice basic searches, display line numbers, use anchors, and harness both basic and extended regular expressions for complex pattern matching with grep.
  2. Linux grep Command: Pattern Searching - Learn to use grep for searching and matching patterns within text files, and explore regular expressions to define complex search patterns.
  3. Needle in the Haystack - Learn the power of the grep command to search for specific patterns, count occurrences, extract unique values, and combine multiple search criteria across various log files.

Lesson complete

You finished grep

You can now search line-oriented text and distinguish matches from errors.

  • Choose basic, extended, or fixed-string matching.

  • Quote patterns and use -e for a leading hyphen.

  • Count selected lines without confusing them with occurrences.

  • Filter stdin or recursively search a focused directory.

  • Interpret exit statuses for match, no match, and error.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Back to Text-Fu