grep

Linux · Text · available in the simulator

What it does

Search for a pattern in one or more files.

Syntax

grep [opciones] patrón [archivo...]

Options and parameters

  • -i

    Perform case-insensitive matching.

    grep -i error log.txt
  • -r

    Search recursively within directories.

    grep -rin "error" .
  • -n

    Prefix output with the line number of each match.

  • -v

    Invert match to select non-matching lines.

    grep -v "^#" config.conf
  • -E

    Use extended regular expressions; equivalent to egrep.

    grep -E "error|warn" log.txt
  • -c

    Print only the count of matching lines.

  • -R

    Like -r, but follow all symbolic links.

  • -l

    Print only the names of files with matches.

  • -w

    Match only whole words, not partial matches.

  • -o

    Print only the matched parts of a line.

  • -F

    Treat pattern as a literal string; equivalent to fgrep.

  • -A

    Print the specified number of trailing context lines.

    grep -A3 error log.txt
  • -B

    Print the specified number of leading context lines.

  • -C

    Print the specified number of leading and trailing context lines.

  • --include

    Limit recursive search to files that match the pattern.

  • --exclude

    Skip files that match the pattern from search.

Usage examples

  • grep error archivo.txt
  • grep -i error log.txt
  • grep -rn TODO src/
  • grep error app.log
  • grep -rin 'error' .
  • grep -rin "error" .
  • grep -v "^#" config.conf
  • grep -E "error|warn" log.txt
  • grep -A3 error log.txt
Practise grep

Related commands