grep
In this page:
grep
grep searches input for lines matching a pattern and prints the matching lines. Common flags include -i (case-insensitive), -v (invert match), -n (show line numbers), and -r (recursive over a directory). It's one of the most frequently used text-processing tools in shell scripting.
Note: grep -v pattern prints every line that does *not* match — useful for filtering out noise.
Example: grep
#!/bin/bash
printf "apple\nbanana\ncherry\navocado\n" > fruits.txt
echo "Lines containing 'a':"
grep "a" fruits.txt
echo "Lines starting with 'a':"
grep "^a" fruits.txt
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: