Grep-Command Linux

bob218
2 min readMay 21, 2024

--

of bob218

Image of bob218

Grep is a command-line utility used in Unix and Unix-like operating systems to look for patterns in text files and command output. Its name stands for “Global Regular Expression Print.”

Basic syntax:

> Pattern: The text pattern you want to search for. It can be a simple string or a regular expression.
> [file …]: Optional parameter that specifies the file or files in which to search. If not provided, grep reads from standard input.

Examples:

> Ignores case distinctions in both the search model and input files.

grep -i "hello" file.txt

> Reverses the direction of matching by selecting mismatched lines.

grep -v "error" file.txt

> Prefixes each output line with the line number based on 1 within the input file.

grep -n "pattern" file.txt

> Recursively search the listed subdirectories.

grep -r "pattern" /path/to/directory

> It suppresses normal output; instead, it prints a corresponding line count for each input file.

grep -c "pattern" file.txt

> Suppresses normal output and prints the name of each input file from which normal output would have been printed.

grep -l "pattern" *

> Selects only rows containing matches that form whole words.

grep -w "word" file.txt

> It interprets the model as an extended regular expression (ERE).

grep -E "pattern1|pattern2" file.txt

> Prints a number of context lines after the corresponding lines.

grep -A 3 "pattern" file.txt

> Print a number of context rows before the corresponding rows.

grep -B 2 "pattern" file.txt

> Highlights the matching text.

grep - color=auto "pattern" file.txt

For more detailed information, you can refer to the grep manual by running man grep in your terminal. These usage possibilities make grep a versatile tool for searching and manipulating text files.

PS: I hope this content was useful and thank you for reading:)

💻 Follow me
👏 Give the article 50 claps
📚
Read more articles on Medium
🔗 Connect on social media
Github | Kaggle

--

--

bob218
bob218

No responses yet