Quick grep reference and tutorial.

Here’s a short but quite comprehensive grep reference and tutorial.

Snippet:

Search for match (the string ‘hello’) in file (called generically ‘file’). Display every line that matches pattern (in this case every line containing ‘hello’)
grep hello file

Search for match in file and use quotes on the pattern. Not required unless you have special chars that are expanded by the shell. (in this case not required)
grep ‘hello’ file

Search for a match in multiple files
grep hello file1 file2

Search for match in all files in current dir (will show a warning if dirs are present too)
grep hello *

Search for a match in all files in curent dir. Don’t show errors if dirs are present. (grep treats dirs just as ordinary files and tries to “read” them). ‘-s’ is for silent. Will also skip errors regarding nonexistent files.
grep -s hello *

Search for a match in all files than end with ‘.py’
grep hello *.py

Leave a Reply

Your email address will not be published. Required fields are marked *