Babak K. Shandiz
Babak K. Shandiz's Blog

Babak K. Shandiz's Blog

Living in the Shell #2; grep (Pattern Matching) (Part 1)

Living in the Shell #2; grep (Pattern Matching) (Part 1)

Babak K. Shandiz's photo
Babak K. Shandiz
·Nov 25, 2021·

1 min read

grep🎖️

Prints/filters lines that match a Regular Expression (RE) pattern.

Filter constant

echo -n 'Hello World!\nI''m Going!\nGoodbye!' | grep "Good"
  Goodbye!

Filter file content

cat ~/.bashrc | grep "alias"
grep "alias" ~/.bashrc

Filter pattern, with PERL flavour -P

cat ~/.bashrc | grep -P "^#"

Prints lines beginning with #.

Filter case-insensitive -i

echo -n 'Hello World!\nI''m Going!\nGoodbye!' | grep -i "go"
  I'm Going!
  Goodbye!

Exclude -v

echo -n 'Hello World!\nI''m Going!\nGoodbye!' | grep -i -v "go"
  Hello World!
 
Share this