grep

grep finds string patterns within a given file or folder.

$ grep PATTERN dir/file.name
$ grep 'THIS THAT' dir/file.name

Regular Expressions

Square brackets do the same as normal, and also includes special POSIX groups, that do what you assume: [:alnum:], [:alpha:], [:blank:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].

Search with OR, AND, NOT

OR

$ grep 'PATTERN1\|PATTERN2' dir/file.name

AND

$ grep PATTERN1 dir/file.name | grep PATTERN2

NOT

$ grep -v 'NOTPATTERN' dir/file.name

Chain together a NOT with a regular search

$ grep 'THIS' dir/file.name | grep -v "NOT THIS'

Options

Options with a single hyphen can be combined and placed after grep. Double hyphen options are separated.

$ grep -rin PATTERN file.diz

Output Formatting

You can use awk to print out a formatted output. -F and the following char is where in the string awk will split. Each split section can be called using $NF, with NF being the number of the field we want. $0 is everything.

grep IDEA | awk -F: '{print $1}'

References

  1. https://www.man7.org/linux/man-pages/man1/grep.1.html

Last modified: 202105310508