DEV Community

Cover image for Living in the Shell #2; grep (Pattern Matching) (Part 1)
Babak K. Shandiz
Babak K. Shandiz

Posted on • Originally published at babakks.github.io on

4 2

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

grep🎖️

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

Filter constant

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

Filter file content

cat ~/.bashrc | grep "alias"
Enter fullscreen mode Exit fullscreen mode
grep "alias" ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Filter pattern, with PERL flavour -P

cat ~/.bashrc | grep -P "^#"
Enter fullscreen mode Exit fullscreen mode

Prints lines beginning with #.

Filter case-insensitive -i

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

Exclude -v

echo -n 'Hello World!\nI''m Going!\nGoodbye!' | grep -i -v "go"
Enter fullscreen mode Exit fullscreen mode
  Hello World!

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay