Prints/filters lines that match a Regular Expression (RE) pattern.
Print line numbers
echo -n 'Hello World!\nI''m Going!\nGoodbye!' | grep -n "G."
2:Im Going! 3:Goodbye!
Print lines around (after/before) every match
cat ~/.bashrc | grep "if" -A 5 -B 1
Prints next 5 lines and previous 1 line when caught any “if”.
Only check silently (no output)
cat ~/.bashrc | grep -q "alias"
0exit code on any match, otherwise
Count matching lines
cat ~/.bashrc | grep -c "alias"
Print only matching substring
echo -n 'Hello World!\nI''m Going!\nGoodbye!' | grep -o "G..d"
About Living in the Shell
Obsessed with doing things in the shell, I’ve decided to share my daily struggles on living in the shell as terse but informative posts.