The grep command in Linux: look for patterns in text

Introduction

The commandgrep(Global Regular Expression Print) is one of the most powerful and used tools in the Linux command line environment. It allows you to search within files or text flows for any pattern that matches a regular expression, returning only the lines that meet the specified criterion. Its simplicity and speed make it an indispensable ally for system administrators, developers and any user who needs to filter information quickly.

Basic syntax

The simplest way to usegrepis:

grep [opciones] patrón archivo

If a file is not indicated,grepRead from the standard input, which allows you to chain it with other commands by piping (|). The pattern can be a literal chain or a more complex regular expression.

Most commonly used options

  • -i: ignores capital and lower capital.
  • -v: reverse the coincidence, showing the lines that do NOT contain the pattern.
  • -c: counts the number of lines that match.
  • -n: shows the line number together with each match.
  • -ror-R: conducts a recursive search in directories.
  • -l: list only the names of the files containing at least one match.
  • -w: matches only full words.
  • -E: interprets the pattern as an extended regular expression (equivalent toegrep).
  • -F: treats the pattern as a fixed chain, without interpreting metacaracterms (equivalent tofgrep).

Practical examples

  • Find a word in a file:
  • grep 'error' /var/log/syslog
  • Ignore capital letters and show line number:
  • grep -in 'usuario' /etc/passwd
  • Tell how many times an IP address appears in a log:
  • grep -c '192\.168\.1\.' access.log
  • Show lines that do NOT contain the word «debug»:
  • grep -v 'debug' aplicación.log
  • Recursively search all .conf files in a directory:
  • grep -r 'puerto' /etc/nginx/
  • Use extensive regular expressions to find dates in AAAA-MM-DD format:
  • grep -E '[0-9]{4}-[0-9]{2}-[0-9]{2}' reporte.txt
  • Show only the names of the files that contain the word «ALL»:
  • grep -rl 'TODO' src/

Advanced trout

  • Combine withawkorsed:after filtering withgrep, you can process the output with other tools to extract columns or perform replacements.
  • Use context expressions: -A n(after),-B n(before) and-C n(context) show back, previous or both lines around the coincidence.
  • grep -C 2 'falló' proceso.log
  • Search in compressed files without uncompress: zgrepworks directly on.gz.
  • Exclude certain directories:with--exclude-dircan omit folders like.gitornode_modulesin recursive searches.
  • grep -r 'clase' . --exclude-dir={.git,node_modules}
  • Coloring the output:the option--color=autohighlights the coincidences, facilitating the reading of terminals that support ANSI.

Conclusion

Domaingrepallows time and effort to be saved by working with large volumes of text, either to debug applications, review configurations or analyze logs. Its combination of simplicity, power and flexibility makes it an essential command that every Linux user should have in his toolbox. Practice with the options and examples presented will make the search for patterns an almost intuitive task.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish