@@ -59,4 +59,69 @@ grep [options] pattern [files]
5959| ` -E ` | ` --extended-regexp ` | Treats pattern as an extended regular expression (ERE) |
6060| ` -w ` | ` --word-regexp ` | Match whole word |
6161| ` -o ` | ` --only-matching ` | Print only the matched parts of a matching line, with each such part on a separate output line. |
62- | | ` --line-buffered ` | Force output to be line buffered. |
62+ | | ` --line-buffered ` | Force output to be line buffered.
63+
64+ ### Additional Practical Examples:
65+
66+ 6 . Search recursively in all files within a directory
67+
68+ ``` bash
69+ grep -r " pattern" /path/to/directory
70+ ```
71+
72+ 7 . Search for whole words only (not partial matches)
73+
74+ ``` bash
75+ grep -w " key" destination.txt
76+ ```
77+
78+ 8 . Display lines that do NOT contain the pattern (invert match)
79+
80+ ``` bash
81+ grep -v " unwanted" destination.txt
82+ ```
83+
84+ 9 . Search for multiple patterns using extended regex
85+
86+ ``` bash
87+ grep -E " pattern1|pattern2" destination.txt
88+ ```
89+
90+ 10 . Search and show context lines (lines before and after match)
91+
92+ ``` bash
93+ # Show 2 lines before and 2 lines after each match
94+ grep -C 2 " error" logfile.txt
95+
96+ # Show only 3 lines before each match
97+ grep -B 3 " error" logfile.txt
98+
99+ # Show only 3 lines after each match
100+ grep -A 3 " error" logfile.txt
101+ ```
102+
103+ 11 . Search for pattern at the beginning or end of line
104+
105+ ``` bash
106+ # Lines starting with "Error"
107+ grep " ^Error" logfile.txt
108+
109+ # Lines ending with ".txt"
110+ grep " \.txt$" filelist.txt
111+ ```
112+
113+ 12 . Count total matches across multiple files
114+
115+ ``` bash
116+ grep -c " pattern" file1.txt file2.txt file3.txt
117+ ```
118+
119+ 13 . Find files containing a pattern and pipe to other commands
120+
121+ ``` bash
122+ # Find all Python files containing "import numpy"
123+ grep -r " import numpy" --include=" *.py" .
124+
125+ # Search in compressed log files
126+ zgrep " error" /var/log/syslog.* .gz
127+ ` ` ` |
0 commit comments