grep Global Regular Expressions Print linux/mac

tips & tricks
Post Reply
dev
Site Admin
Posts: 60
Joined: 09 Mar 2021, 17:52

grep Global Regular Expressions Print linux/mac

Post by dev »

grep Global Regular Expressions


https://www.youtube.com/watch?v=VGgTmxXp7xQ

Code: Select all

grep "John Williams" names.txt
-i	ignore case
-v	invert match
-w	select only those lines containing matches that form whole words
-n	line number
-B 2	+two lines before
-A 3	+three lines after
-C 2	+two before and after line (context)
-l	search and list filenames contains it
-c	counts matches
-P	regular expressions

Code: Select all

grep -win "John Williams" ./*	search in all files in the directory (no subdirs)
grep -win "John Williams" ./*.txt	search in all txt files in the directory (no subdirs)
grep -winr "John Williams" ./*.txt	search in all txt files in the directory (subdirs with r option, recursive)
grep -wirl "John Williams" .	search and list filenames contains it (l option, list files)
grep -wirc "John Williams" .	search and counts matches in every file
grep -P "\d{3}-\d{3}-\d{4}" names.txt
Attachments
Grep.zip
(4.51 KiB) Downloaded 704 times
Post Reply