Skip to content

grep

used standalone : list files
grep [option] [pattern] [file/path]

most common use case :
grep -i -r 'string-patern' 'file/path'

vergelijk
ack = grep variant in perl : https://www.maketecheasier.com/ack-a-better-grep/

common aliasses

( depreciated , but still available )
egrep = grep -E
fgrep = grep -F

cmdline options

-i ignore case
-r recursive
-R idem-r , but also follow the symlinks

zoek alle files met patroon nl_BE.UTF-8 erin , in het gans filesystem :
sudo grep -r nl_BE.UTF-8 /

to print only X lines before (-B) and after the match :
-B
-A
grep -i -B 10 ‘error’ data
print the 10 lines before the match, including the matching line itself.

grep -rl tezoekenstring .
vind alle files met
“tezoekenstring” enkel in de filename (optie -l)
in de huidige folder (“.”)
en alle subfolders ervan (-r)

grep -r -l --include={*.pdf,*.docx} "search term" /path/to/base/search/directory/

–include={.pdf,.docx} => optional, only search in pdf and docx files. (you can add more)
“search term” => search term with quotes
/path/to/base/search/directory/ => optional, but without this you should cd into the base directory of your search before running grep.

grep tezoekenstring /mnt/data/PKS/nota
vind alle files met
“tezoekenstring” in de inhoud of filename [geen -l optie, dan in beide]
in de folder /mnt/data/PKS/nota

https://www.techrepublic.com/index.php/article/10-ways-to-use-grep-to-search-files-in-linux/

egrep

is an alias to grep -E
which allows to (? regex) use expressions in the search string

iostat  | egrep "^Device:|sd*"

ex. grep oftentimes loses the header row , but with egrep you can add it
compare :

iostat  | egrep "^Device:|sd*"

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
sda               7,58       115,29       122,49         0,00  268709798  285492660          0
sdb               4,07       128,32        28,45        31,56  299093292   66318473   73565852
sde             196,46         0,39     12570,51         0,00     913672 29299392136          0
sdf               0,04         4,65         0,00         0,00   10848952       3368          0

iostat  | grep "sd"

sda               7,58       115,29       122,49         0,00  268709798  285493084          0
sdb               4,07       128,32        28,45        31,56  299093304   66318641   73565852
sde             196,46         0,39     12570,39         0,00     913672 29299392136          0
sdf               0,04         4,65         0,00         0,00   10848952       3368          0