Grep command series: How to Use grep commands in Linux/UNIX
How can I use the grep command in Linux, Apple OS X, and other UNIX-like systems? Can you give me some simple examples?
The grep command is used to search for text, or to search for a file containing a given string or word in a row from a given file. In general, grep displays the matched rows. Use grep to search for text rows that match one or more regular expressions, and then show only matched rows. Grep is regarded as one of the most useful commands in Linux/Unix systems.
Do you know
The grep name comes from the command for executing similar operations in the old line editor ed in Unix/Linux:
g/re/p
Grep command syntax
The syntax is as follows:
Grep 'word' file name grep 'word' file 1 file 2 file 3 grep 'string 1 string 2' file name cat a file | grep 'something 'COMMAND | grep' something 'COMMAND option 1 | grep 'data' grep -- color 'data' file name
How to Use grep to search for a file
Search for boo users in the/etc/passwd file and enter:
$ grep boo /etc/passwd
Output content:
foo:x:1000:1000:foo,,,:/home/foo:/bin/ksh
You can use grep to forcibly ignore case sensitivity. For example, you can use the-I option to match boo, Boo, BOO, and other combinations:
$ grep -i "boo" /etc/passwd
Recursive grep
You can use grep recursively for search. For example, search for all files containing the string "192.168.1.5" under the file directory.
$ grep -r "192.168.1.5" /etc/
Or:
$ grep -R "192.168.1.5" /etc/
Sample output:
/etc/ppp/options:# ms-wins 192.168.1.50/etc/ppp/options:# ms-wins 192.168.1.51/etc/NetworkManager/system-connections/Wired connection 1:addresses1=192.168.1.5;24;192.168.1.2;
You will see the result of searching for 192.168.1.5 with a prefix for each row to find the matching file name (for example,/etc/ppp/options ). You can add the-h option to the file name contained in the output to disable the output:
$ grep -h -R "192.168.1.5" /etc/
Or
$ grep -hR "192.168.1.5" /etc/
Sample output:
# ms-wins 192.168.1.50# ms-wins 192.168.1.51addresses1=192.168.1.5;24;192.168.1.2;
Use grep to search for text
When you search for boo, The grep command will match fooboo, boo123, barfoo35, and all other strings containing boo, you can use the-w option to forcibly output only the rows that contain the entire word (LCTT: that is, the two sides of the string are English word delimiters, such as spaces and punctuation marks, it is not applicable to Chinese languages without break characters .).
$ grep -w "boo" file
Use the grep command to search for two different words
The egrep command is as follows:
$ egrep -w 'word1|word2' /path/to/file
(LCTT: here the regular expression is used, so the egrep command is used, that is, the extended grep command .)
Count the number of lines matching the text
The grep command can display the number of matching times in each file by adding the-c parameter:
$ grep -c 'word' /path/to/file
The line number of the row to be matched is added before the row to be output by passing the-n option:
$ grep -n 'root' /etc/passwd
Sample output:
1:root:x:0:0:root:/root:/bin/bash1042:rootdoor:x:0:0:rootdoor:/home/rootdoor:/bin/csh3319:initrootapp:x:0:0:initrootapp:/home/initroot:/bin/ksh
Reverse match (mismatch)
You can use the-v option to output the content that does not contain a match. The output content only contains rows that do not contain the given word. For example, output all rows that do not contain the bar word:
$ grep -v bar /path/to/file
UNIX/Linux pipelines and grep commands
Grep is often used with pipelines. In this example, the name of the hard disk device is displayed:
# dmesg | egrep '(s|h)d[a-z]'
Display CPU model:
# cat /proc/cpuinfo | grep -i 'Model'
However, the preceding commands can also be used as follows without using pipelines:
# grep -i 'Model' /proc/cpuinfo
Sample output:
model : 30model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHzmodel : 30model name : Intel(R) Core(TM) i7 CPU Q 820 @ 1.73GHz
How does one display only the name of the file that matches the content?
Use the-l option to display the file names whose content contains main:
$ grep -l 'main' *.c
Finally, you can force grep to output in color:
$ grep --color vivek /etc/passwd
Sample output:
Grep command in action
Grep uses concise and Regular Expressions
Regular Expression usage
Assertion with Zero Width of a regular expression
Linux Command-grep for file text operations
Grep Regular Expression
Regular Expressions and file formatting commands in Linux (awk/grep/sed)
Via: http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
Author: Vivek Gite Translator: zky001 Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China
This article permanently updates the link address: