Tutorial by Examples

Running the command: grep sam someFile.txt When someFile.txt contains: fred 14 m foo sam 68 m bar christina 83 f baz bob 22 m qux Sam 41 m quux Will produce this output: sam 68 m bar
Given a file sample: hello Hello HELLO_there A normal grep for "hello" returns: $ grep "hello" sample hello Using -i allows to ignore case and match any "hello": $ grep -i "hello" sample hello Hello HELLO_there
Given a file sample: hello world ahello here hello_there A normal grep for "hello" returns: $ grep hello sample hello world ahello here hello_there Using -w allows to select those lines containing matches that form whole words: $ grep -w hello sample hello world
Using GNU grep grep -r 'pattern' <directory path> To also list line numbers of matches use -n option grep -rn 'pattern' <directory path> To search only files with particular glob pattern grep --include='*.txt' -r 'pattern' <directory path> Exclude file patterns or direc...
echo "Prints only the matching part of the lines" | grep -o "matching" # prints matching
Given a file Sample called movieslist. Troy Gladiator Robin Hood King Arthur BraveHeart The Last Samurai Normal grep returns grep "Gladiator" movieslist Gladiator Now,using grep to print the below or above lines of the file. To print the below line grep -A 1 Gladiator movie...

Page 1 of 1