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:]
.
OR
$ grep 'PATTERN1\|PATTERN2' dir/file.name
AND
$ grep PATTERN1 dir/file.name | grep PATTERN2
NOT
$ grep -v 'NOTPATTERN' dir/file.name
Chain 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
-h
: suppress filename from output-i
: ignore case of search pattern-l
: print only filename-n
: print line number where pattern is found-r
: search recursively within a folder--color
: color the output where pattern is found--include=GLOB
: search only files whose base name matches GLOB using wildcard matching (e.g.$ grep --include=*.md PATTERN
)--exclude-dir=dir_name
or--exclude-dir={multiple,dir_names}
(be sure to use the curlys)
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
Incoming Links
Last modified: 202401040446