zhaowawj / ks

my knowledge summary
0 stars 0 forks source link

[linux command] grep #2

Open zhaowawj opened 7 years ago

zhaowawj commented 7 years ago

1 search text or searches the given file for lines containing a match to the given strings or words.

2 sample usage

basic

2.1 common grep 'word' filename grep 'word' file1 file2 file3 grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something' command option1 | grep 'data' grep --color 'data' fileName

2.2 ignore case $ grep -i "boo" /etc/passwd

2.3 recursively $ grep -R "192.168.1.5" /etc/

2.4 When you search for boo, grep will match fooboo, boo123, barfoo35 and more. You can force the grep command to select only those lines containing matches that form whole words i.e. match only boo word: $ grep -w "boo" file

2.5 Use grep to search 2 different words $ egrep -w 'word1|word2' /path/to/file

2.6 The grep can report the number of times that the pattern has been matched for each file using -c (count) option: $ grep -c 'word' /path/to/file

2.7 Pass the -n option to precede each line of output with the number of the line in the text file from which it was obtained: $ grep -n 'root' /etc/passwd

2.8 You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar: $ grep -v bar /path/to/file

2.9 Use the -l option to list file name whose contents mention main(): $ grep -l 'main' *.c