The grep command is a command that is used very frequently in Linux, whether it is used in daily terminal operations or programming. The following is an introduction with examples.
1. Basic grammar
grep [options] PATTERN [FILE...]
Common parameters:
-i: Ignore case (-i has this meaning in many commands);
-v, --invert-match: reverse display, display all lines that do not contain matching text;
-R, -r, --recursive: Recursively read all files in each directory. Equivalent to -d recurse option;
-o, --only-matching: Only display the part that matches PATTERN in the matched line;
-n, --line-number: add its line number in the file where it is located in front of each output line;
--color: Highlight the matched content. This parameter is usually added by default in grep;
-A NUM, --after-context=NUM: print out the following NUM lines immediately after the matched line;
-B NUM, --before-context=NUM: print out the above NUM lines before the matched line;
-C NUM, --context=NUM: print out the NUM lines before and after the context of the matched line;
2. Examples
2.1 No parameters
In the current directory, find the file containing the string "hosts", as shown below:
[root@localhost ssh]# grep "hosts" ./*
./ssh_config:# RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#
In the search result, the file is listed first, and then the line where the string "hosts" is located is output. By default, the matched content is highlighted, such as "hosts" here.
Check the alias of the command in the system, as shown below:
[root@localhost ssh]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ssh]#
Among them, the command grep is equal to grep --color=auto, and the --color parameter has been added by default.
2.2 -r parameter
In the current directory, recursively search for files containing the string "hosts", that is, search for the current directory and all directories under the current directory, and search recursively. As follows:
[root@localhost etc]# grep -r "hosts" ./*
./avahi/hosts:# See avahi.hosts(5) for more information on this configuration file!
./cupshelpers/preferreddrivers.xml: <drivertype name="ghostscript">
./cupshelpers/preferreddrivers.xml: <attribute name="ppd-product" match=".*Ghostscript"/>
./cupshelpers/preferreddrivers.xml: <drivertype>ghostscript</drivertype>
./dnsmasq.conf:# from /etc/hosts or DHCP only.
./dnsmasq.conf:# If you don't want dnsmasq to read /etc/hosts, uncomment the
./dnsmasq.conf:#no-hosts
./dnsmasq.conf:# or if you want it to read another file, as well as /etc/hosts, use
./dnsmasq.conf:#addn-hosts=/etc/banner_add_hosts
./dnsmasq.conf:# automatically added to simple names in a hosts-file.
./dnsmasq.conf:#expand-hosts
……
./tcsd.conf:# on this machine's TCSD by TSP's on non-local hosts (over the internet).
./yum/pluginconf.d/fastestmirror.conf:hostfilepath=timedhosts.txt
[root@localhost etc]#
2.3 -i parameter
Find the file containing the string "hosts" in the current directory, and the characters in the string are not case sensitive, as shown below:
[root@localhost ssh]# grep -i "hosts" ./*
./ssh_config:# HOSTS tmp
./ssh_config:# RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#
In the above search results, HOSTS, Hosts, and hosts have all been found.
2.4 -o parameter
Find the file containing the string "hosts" in the current directory, and only display the same content as "hosts". As follows:
[root@localhost ssh]# grep -o "hosts" ./*
./ssh_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
[root@localhost ssh]#
Among them, the displayed matching content only shows the same content as "hosts".
2.5 -n parameter
Find the file containing the string "hosts" in the current directory, and display the line number of the matching content, as shown below:
[root@localhost ssh]# grep -n "hosts" ./*
./ssh_config:24:# RhostsRSAAuthentication no
./sshd_config:54:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:56:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:59:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:60:#IgnoreRhosts yes
[root@localhost ssh]#
The line number is added before the match.
2.6 -A, -B, -C parameters
-A: Print out the following 1 line immediately after the matched line, as shown below:
[root@localhost ssh]# grep -A 1 "hosts" ./*
./ssh_config:# RhostsRSAAuthentication no
./ssh_config-# RSAAuthentication yes
--
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
--
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
[root@localhost ssh]#
Among them, "--" means that a line of - will be printed between adjacent matching groups. If two matching groups are closely connected in the actual file, there will be no "--" separation.
-B: Print out the above 2 lines before the matched line, as shown below:
[root@localhost ssh]# grep -B 2 "hosts" ./*
./ssh_config-# ForwardAgent no
./ssh_config-# ForwardX11 no
./ssh_config:# RhostsRSAAuthentication no
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#
-C: Print out 2 lines before and after the context of the matched line, as shown below:
[root@localhost ssh]# grep -C 2 "hosts" ./*
./ssh_config-# ForwardAgent no
./ssh_config-# ForwardX11 no
./ssh_config:# RhostsRSAAuthentication no
./ssh_config-# RSAAuthentication yes
./ssh_config-# PasswordAuthentication yes
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
./sshd_config-# To disable tunneled clear text passwords, change to no here!
[root@localhost ssh]#
Three, summary
grep is a command often used in Linux terminal operations. It is usually used to find out which files contain specified content.
references:
[1] Linux grep manual;