GREP
grep (Global search Regular Expression and Print out the line) is a powerful text search tool, it can use a regular expression search text, and print out the matching rows.
grep example
grep 'word' filename
: Search for files contain word lines;grep -i 'bar' file1
: Case-insensitive search bar in the file;grep -R foo
: Search for the word foo in the current folder and its subfolders;grep -c 'nixcraft' frontpage.md
: Search for and displays the number of 'nixcraftq' frontpage.md appear in the file;
Linux and unix syntax of
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
Use grep search files in linux
In the / etc / passwd file search boo:
grep boo /etc/passwd
Case-insensitive search boo, Boo BOO and so on:
grep -i "boo" /etc/passwd
Finally grep command may follow the cat:
cat /etc/passwd | grep -i "boo"
Iteration
-R or -r parameter can be iterated to find in folders and sub-folders
grep -r "192.168.1.5" /etc/
Returns
/etc/ppp/options:# ms-wins 192.168.1.50
/etc/ppp/options:# ms-wins 192.168.1.51
-h parameter can omit the filename, -n parameter display line numbers:
Only a single word search
When using grep search boo, returned may have fooboo, boo123 and so on. -w parameter can only search a boo word:
grep -w "boo" file
Search two different words
Use egrep name:
egrep -w 'word1|word2' file
The number of rows to be displayed matching file
The -c parameter, grep can report the number of lines in each file matches
grep -c 'word' /path/to/file
No match display
The -v argument can not match the print line:
grep -v 'bar' /path/to/file
linux pipes and grep
grep parameters may be used in pipes, use the |
command.
For example cpu print mode
cat /proc/cpuinfo | grep -i 'Model'
It may be combined apt search
to find the installation package
Listed successfully matched filenames
Use the -l command to list the file name contains the main () is:
grep -l 'main' *.c
to sum up
parameter | description |
---|---|
-i | Ignore case |
-n | Only whole words |
-v | Select not match mode |
-n | Print line numbers |
-h | Does not display the file name |
-r | Iteration |
-R | Iteration |
-l | Print only successful match the file name |
-c | The number of lines to print documents only successfully matched |
--color | Use color display successful match |