grep is a command-line utility used to search for specific patterns within text. When used in pipelines, it takes input from the output of the previous command and filters it based on the specified pattern. Here's how it works:
-
Basic Syntax:
command | grep 'pattern' -
How it Works:
- The
commandgenerates output, which is then passed togrep. grepscans the input line by line and checks for the presence of the specifiedpattern.- If a line matches the pattern,
grepoutputs that line to the terminal.
- The
-
Example:
To find all lines containing the word "error" in a log file:cat logfile.txt | grep 'error' -
Options:
-i: Ignore case (e.g.,grep -i 'error').-v: Invert match (show lines that do not match the pattern).-r: Recursively search through directories.
Using grep in pipelines is a powerful way to filter and process text data efficiently.
