A command to extract lines with a specific character string (pattern) in a file One of the most commonly used Linux commands
grep [Option] Search pattern file name[file name] #file nameは複数指定できる
Or
command| grep [Options] Search pattern
※[]Is optional
● Search related options
option | meaning | Sample code |
---|---|---|
-E | Use extended regular expressions for search | Usage example 1 |
-i | Search case-insensitive | Usage example 2 |
-P | Use Perl regular expressions for searching | Usage example 3 |
-x | Search for an entire line that matches the pattern | Usage example 4 |
● Display related options
option | meaning | Sample code |
---|---|---|
-Number of A lines | Also show the line after the matched line | Usage example 5 |
-b | Display how many characters are in front of the pattern | Usage example 6 |
-Number of B lines | Also show the line before the matched line | Usage example 7 |
-c | Show only the number of times that a match is included | Usage example 8 |
-Number of C lines | Also display the lines before and after the matched line | Usage example 9 |
-h | Do not display the file name (when multiple files are specified) | Usage example 10 |
-H | Display the file name together | Usage example 11 |
-l | Show only filenames of files that contain matches | Usage example 12 |
-L | Show only filenames of files that do not contain a match | Usage example 13 |
-m times | Ends processing when the specified number of lines containing matching patterns are reached | Usage example 14 |
-n | Display line numbers together | Usage example 15 |
-o | Show only matching parts | Usage example 16 |
-q | Do not display the result (mainly used for judgment in shell scripts etc.) | Usage example 17 |
-v | Show lines that do not match the pattern | Use case 18 |
● Search target related options
option | meaning | Sample code |
---|---|---|
-r | If a directory is specified, the files in the subdirectories are also searched. | Usage example 19 |
-R | Search including subdirectories, and further symbolic links | Usage example 20 |
#/etc/When searching the contents of the passwd file with "hoge"
grep hoge /etc/passwd
Execution result
hoge:x:1000:1000::/home/hoge:/bin/bash #Lines containing "hoge" are displayed
#When searching for an arbitrary pattern for the command execution result
cat /etc/passwd | grep HOGE
Execution result
HOGE:x:1001:1001::/home/HOGE:/bin/bash #Only the lines containing "HOGE" are displayed from the command execution result.
##「cat /etc/If only "passwd" is used, the following execution result(Show all the contents of the file)
root:x:0:0::/root:/bin/bash
・
・
・
hoge:x:1000:1000::/home/hoge:/bin/bash
HOGE:x:1001:1001::/home/HOGE:/bin/bash
・
・
・
#When searching by specifying multiple files
/etc/test.txt
aaa
bbb
ccc
ddd
eee
fff
/etc/test1.txt
aaa
aaa
bbb
aaa
bbb
ccc
grep aaa /etc/test.txt /etc/test1.txt
Execution result
test.txt:aaa
test1.txt:aaa
test1.txt:aaa
test1.txt:aaa
#/etc/When searching the contents of the passwd file with a regular expression
grep ^h... /etc/passwd
Execution result
halt:x:7:0:halt:/sbin:/sbin/halt #The first line that contains the four-letter word starting with "h" is displayed
hoge:x:1000:1000::/home/hoge:/bin/bash #See another page for details on regular expressions
__ Use extended regular expressions for search __
#/etc/sysconfig/network-script/Search the contents of the enp0s3 file with an extended regular expression
grep -E "((25[0-5]|1[0-9][0-9]|[0-9]?[0-9])\.){3}(25[0-5]|1[0-9][0-9]|[0-9]?[0-9])" /etc/sysconfig/network-scripts/ifcfg-enp0s3
Execution result
IPADDR=192.168.0.2 #The line containing the IP address is displayed
GATEWAY=192.168.0.1
DNS1=8.8.8.8
__ Search case-insensitive __
#/etc/Search the contents of the passwd file with "hoge" regardless of case
grep -i hoge /etc/passwd
Execution result
hoge:x:1000:1000::/home/hoge:/bin/bash #Lines containing both "hoge" and "HOGE" are displayed regardless of case.
HOGE:x:1001:1001::/home/HOGE:/bin/bash #If there is a line containing "Hoge" or "hogE", that line is also displayed.
__ Express using Perl regular expressions __
#/etc/httpd/conf/httpd.Search the contents of the conf file using Perl regular expressions
grep -P "\d" /etc/httpd/conf/httpd.conf #Search so that lines containing numbers are displayed
Execution result
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed infomation.
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
・
・
・
__ Find if the entire line matches the pattern __
#To check if the entire line contains lines that match the pattern
grep -x IPADDR=192.168.0.2 /etc/sysconfig/network-scripts/ifcfg-enp0s3
Execution result
IPADDR=192.168.0.2 #Only rows where the entire row matches the pattern are displayed
##grep -x IPADDR= /etc/sysconfig/network-scripts/ifcfg-When executed with enp0s3
No execution result#Nothing is displayed if the entire line does not match the pattern
__ Show the line after the matched line __
#/etc/test.When searching txt with A option
/etc/test.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
6 fff
grep -A1 ccc /etc/text.txt
Execution result
3 ccc #The 3rd line that matches the pattern and the 4th line that follows it are displayed.
4 ddd
grep -A2 ccc /etc/text.txt
Execution result
3 ccc #The 3rd line that matches the pattern and the 4th and 5th lines behind it are displayed.
4 ddd
5 eee
__ Display how many characters are in front of the pattern __
#/etc/test.When searching txt with b option
/etc/test.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
6 fff
grep -b fff test.txt
Execution result
20:fff #The number of characters before the character string that matches the pattern is displayed together (line breaks and spaces are included in one character).
__ Show the line before the matched line __
#/etc/teat.When searching txt with B option
/etc/test.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
6 fff
grep -B1 fff /etc/test.txt
Execution result
5 eee #The 6th line that matches the pattern and the 5th line before it are displayed.
6 fff
grep -B2 fff /etc/test.txt
Execution result
4 ddd #The 6th line that matches the pattern and the 4th and 5th lines before it are displayed.
5 eee #「-The number after "B" determines how many lines to display before
6 fff
__ Show only the number of lines that contain a pattern match __
#/etc/test1.When searching txt with the c option
/etc/test1.txt
1 aaa
2 aaa
3 bbb
4 aaa
5 bbb
6 ccc
grep -c aaa /etc/test1.txt
Execution result
3 #Show only the number of lines that contain the one that matches the pattern
__ Show the lines before and after the matched line __
#/etc/test.When searching txt with C option
/etc/test.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
6 fff
grep -C1 eee /etc/test.txt
Execution result
4 ddd #The 5th line that matches the pattern and the 4th and 6th lines before and after it are displayed.
5 eee
6 fff
grep -C2 ddd /etc/test.Search by txt
Execution result
2 bbb #The 4th line that matches the pattern and the 2nd, 3rd, 5th, and 6th lines before and after it are displayed.
3 ccc #「-You can decide how many lines before and after the number after "C"
4 ddd
5 eee
6 fff
__ Don't show filenames (only when searching multiple files) __
#/etc/test.txt and/etc/test1.When searching txt with h option
/etc/test.txt
aaa
bbb
ccc
ddd
eee
fff
/etc/test1.txt
aaa
aaa
bbb
aaa
bbb
ccc
grep -h aaa /etc/test.txt /etc/test1.Search by txt
Execution result
aaa #File name is not displayed
aaa
aaa
aaa
__ The file name is also displayed __
#/etc/test.When searching txt with H option
/etc/test.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
6 fff
grep -H aaa /etc/test.txt
Execution result
test.txt:aaa #The file name is displayed
__ Show only filenames of files that contain matches __
#/etc/test.txt and/etc/test1.When searching txt with the l option
/etc/test.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
6 fff
/etc/test1.txt
1 aaa
2 aaa
3 bbb
4 aaa
5 bbb
6 ccc
grep -l ddd /etc/test.txt /etc/test1.txt
Execution result
test.txt #Only filenames containing those that match the pattern are displayed
__ Show only filenames of files that do not contain a match __
#/etc/test.txt and/etc/test1.When searching txt with L option
/etc/test.txt
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
6 fff
/etc/test1.txt
1 aaa
2 aaa
3 bbb
4 aaa
5 bbb
6 ccc
grep -L ddd /etc/test.txt /etc/test1.txt
Execution result
test1.txt #Only filenames that do not match the pattern are displayed
__ Ends processing when the number of lines containing the pattern matches the specified number is reached __
#/etc/test1.When searching for txt with the m option
/etc/test1.txt
1 aaa
2 aaa
3 bbb
4 aaa
5 bbb
6 ccc
grep -m 2 aaa /etc/test1.txt
Execution result
1 aaa #Since the number of times is specified as 2, only the first and second lines are displayed.
2 aaa
__ Display line numbers together __
#/etc/test.When searching txt with n option
/etc/test.txt
aaa
bbb
ccc
ddd
eee
fff
grep -n ccc /etc/test.txt
Execution result
3 ccc #The line number is also displayed
__ Show only matched parts __
#/etc/test2.When searching for txt with the o option
/etc/test2.txt
aaa bbb ccc
ddd eee fff
ggg hhh iii
grep -o aaa /etc/test2.txt
Execution result
aaa #"Bbb ccc" is not displayed because only the part that matches the pattern is displayed.
__ Do not display the result (mainly used for judgment in shell scripts etc.) __
#/etc/test.When searching txt with q option
/etc/test.txt
aaa
bbb
ccc
ddd
eee
fff
grep -q aaa /etc/test.txt
No execution result#Execution result is not displayed
__ Show lines that do not match the pattern __
#/etc/test.When displaying txt with v option
/etc/test.txt
aaa
bbb
ccc
ddd
eee
fff
grep -v aaa /etc/test.txt
Execution result
bbb #Lines that do not match the pattern are displayed
ccc
ddd
eee
fff
__ If you specify a directory, search including files in subdirectories __
#/etc/test(When searching the directory) with the r option
Assuming a directory structure like the one below
etc-test-testtest(directory)-test1.txt (file)
-test.txt (file)
/etc/test/test.txt
aaa
bbb
ccc
ddd
eee
fff
/etc/test/testtest/test1.txt
aaa
aaa
bbb
aaa
bbb
ccc
grep -r aaa /etc/test
Execution result
test.txt:aaa
testtest/test1.txt:aaa #Subdirectory (testtest)The contents of the file in
testtest/test1.txt:aaa
testtest/test1.txt:aaa
__ Search including subdirectories and even beyond symbolic links __
#/etc/When searching for test with R option
Assuming the following directory structure
etc-test-testtest(directory)-test1.txt
-test.txt
-test.3txt(tteesstt.txt symbolic link)
-tteesstt.txt
/etc/test/test.txt
aaa
bbb
ccc
ddd
eee
fff
/etc/test/testtest/test1.txt
aaa
aaa
bbb
aaa
bbb
ccc
zzz
/etc/tteesstt.txt
xxx
yyy
zzz
grep -R zzz /etc/test
Execution result
test/testtest/test1.txt:zzz #Search and display files in subdirectories and files linked to symbolic links
test/test3.txt:zzz
Recommended Posts