› All Posts › programming › find patterns in files using extended regular expressions | egrep cheat-sheet

find patterns in files using extended regular expressions | egrep cheat-sheet

· 156 words · 1 minute read

Find patterns in files using extended regular expression (supports ?, +, {}, (), and |).

Search for a pattern within a file:

egrep "search_pattern" path/to/file

Search for a pattern within multiple files:

egrep "search_pattern" path/to/file1 path/to/file2 ...

Search stdin for a pattern:

cat path/to/file | egrep search_pattern

Print file name and line number for each match:

egrep --with-filename --line-number "search_pattern" path/to/file

Search for a pattern in all files recursively in a directory, ignoring binary files:

egrep --recursive --binary-files=without-match "search_pattern" path/to/directory

Search for lines that do not match a pattern:

egrep --invert-match "search_pattern" path/to/file

For more information, the manpage for egrep: https://manned.org/egrep .

I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .

Abanoub Hanna
Author

Abanoub Hanna

Software developer and founder of KartBusiness

The author is Abanoub Hanna, a graduate of the Faculty of Science, Department of Microbiology and Biochemistry - Sohag University. I work in the software development industry. Founder of the KartBusiness platform, and developer of the Keme PDF app , Keme POS app , Keme Cash app , img2txt OCR app , Agpeya app , Pope Shenouda III Books app , and Keme OCR Text Scanner app , as well as the owner of the Abanoub Hanna website.