grep 命令是 Linux 使用頻率非常高的一個命令,不管是在日常終端操作中,還是在編程中都會用到,下面結合實體進行介紹,
一、基本語法
grep [options] PATTERN [FILE...]
常用引數:
-i : 忽略大小寫(在許多命令中 -i 都是這個含義);
-v ,--invert-match : 反向顯示,顯示不包含匹配文本的所有行;
-R,-r,--recursive :遞回地讀每一目錄下的所有檔案,和 -d recurse 選項等價;
-o, --only-matching :只顯示匹配的行中與 PATTERN 相匹配的部分;
-n, --line-number:在輸出的每行前面加上它所在的檔案中它的行號;
--color :將匹配的內容以高亮顯示,一般 grep 中默認已經加上了該引數;
-A NUM, --after-context=NUM:列印出緊隨匹配的行之后的下文 NUM 行;
-B NUM, --before-context=NUM:列印出匹配的行之前的上文 NUM 行;
-C NUM, --context=NUM:列印出匹配的行的背景關系前后各 NUM 行;
二、實體
2.1 無引數
在當前目錄下,查找包含字串 “hosts” 的檔案,如下所示:
[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]#
在查找結果中,先是列出了所在的檔案,然后輸出字串 “hosts” 所在的行,默認情況下匹配的內容都高亮顯示,比如:這里的“hosts”,
查看下系統中命令的別名,如下所示:
[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]#
其中,命令 grep 等于 grep --color=auto,已默認添加了 --color 引數,
2.2 -r 引數
在當前目錄下,遞回查找包含字串“hosts” 的檔案,即:查找當前目錄以及當前目錄下的所有目錄,遞回查找,如下所示:
[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 引數
查找當前目錄下,包含字串“hosts”的檔案,且字串中的字符不區分大小寫,如下所示:
[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]#
在上面的查找結果中,HOSTS、Hosts、hosts 都被查找出來了,
2.4 -o 引數
查找當前目錄下,包含字串“hosts”的檔案,且僅顯示與“hosts”一樣的內容,如下所示:
[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]#
其中,顯示的匹配內容僅顯示了與 “hosts” 相同的內容,
2.5 -n 引數
查找當前目錄下,包含字串“hosts”的檔案,并顯示匹配內容的行號,如下所示:
[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]#
在匹配內容前加了行號,
2.6 -A,-B,-C引數
-A : 列印出緊隨匹配的行之后的下文 1 行,如下所示:
[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]#
其中,“--” 表示相鄰的匹配組之間會列印 -- 的一行,如果兩個匹配組在實際檔案中位置緊連著,將不會有 “--” 分隔,
-B:列印出匹配的行之前的上文 2 行,如下所示:
[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:列印出匹配的行的背景關系前后各 2 行,如下所示:
[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]#
三、總結
grep 是 Linux 終端操作中經常使用的一個命令,通常用于查找哪些檔案包含指定的內容,
參考文獻:
[1] Linux grep 手冊;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/258119.html
標籤:其他
上一篇:Julia1.6的更新程序
