How to use the Linux grep command

What is the __grep command __

A command to extract lines with a specific character string (pattern) in a file One of the most commonly used Linux commands

__Basic format __

grep [Option] Search pattern file name[file name] #file nameは複数指定できる

Or

command| grep [Options] Search pattern
※[]Is optional

option

● 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

Basic usage example

#/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
  

Usage example 1

__ 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

Usage example 2

__ 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.

Usage example 3

__ 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>
・
・
・

Usage example 4

__ 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
    

Usage example 5

__ 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
 

Usage example 6

__ 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).

Usage example 7

__ 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
 

Usage example 8

__ 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

Usage example 9

__ 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

Usage example 10

__ 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

Usage example 11

__ 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

Usage example 12

__ 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

Usage example 13

__ 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

Usage example 14

__ 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

Usage example 15

__ 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

Usage example 16

__ 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.

Usage example 17

__ 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

Use case 18

__ 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


Usage example 19

__ 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

Usage example 20

__ 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

How to use the Linux grep command
[Linux] How to use the echo command
Linux user addition, how to use the useradd command
How to use the grep command and frequent samples
(Remember quickly) How to use the LINUX command line
How to use the decorator
How to use the zip function
How to use the optparse module
How to use MBDyn (command setting)
[linux] kill command to kill the process
How to use the ConfigParser module
Add Linux user, how to use useradd command (password specification)
How to use the Spark ML pipeline
[python] How to use __command__, function explanation
How to calculate Use% of df command
How to operate Linux from the console
How to use CUT command (with sample)
How to use the IPython debugger (ipdb)
3 best ways to use the less command
How to output the output result of the Linux man command to a file
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use the C library in Python
How to use MkDocs for the first time
How to use the graph drawing library Bokeh
How to use the Google Cloud Translation API
How to operate Linux from the outside Procedure
How to use the NHK program guide API
[Algorithm x Python] How to use the list
How to create a shortcut command for LINUX
[C language] How to use the crypt function on Linux [Password hashing]
How to create an article from the command line