TECH

4-18 Linux中搜索文件的内容 --- grep

2022-03-12  本文已影响0人  捌千里路雲和月

1、grep:用于查找文件里符合条件的字符串(内容)。丰富的参数用于对搜索过程的补充。命令模式十分灵活,可以是变量、字符串、正则表达式。

2、linux 支持 grep、egrep 和 fgrep。grep 和 egrep 都支持正则表达式,只不过 egrep 支持的是扩展正则表达式。fgrep 不支持正则表达式,只支持普通字符串的过滤。

3、grep 加上相应的参数可以实现 egrep 和 fgrep 的功能。所以,也可以用 grep 加上对应的参数来执行 egrep 和 fgrep。

4、通过 man grep 查看说明。

grep、egrep、fgrep 的 功能 grep 命令格式

5、grep 命令的基本操作:

输入内容含 abc 会标红显示

②、grep 根据条件过滤文件内容。

[root@localhost ~]# echo $USER    ## USER 是一个变量,表示当前的用户名
root
[root@localhost ~]# 
[root@localhost ~]# USER=torres    ## USER 赋值为 torres
[root@localhost ~]# 
[root@localhost ~]# echo $USER    ## USER 当前值是 torres
torres

## grep 在 /etc/passwd 目录过略 USER 变量的值
[root@localhost ~]# grep $USER /etc/passwd    
torres:x:1000:1007::/home/torres:/bin/bash    ## 含有 torres 的行也会被列出来
[root@localhost ~]#
[root@localhost ~]# 
[root@localhost ~]# whoami    ##  whoami 是个命令,查询当前的用户
root
[root@localhost ~]# grep `whoami` /etc/passwd    ## `whoami`的结果会被 grep 调用
root:x:0:0:root:/root:/bin/bash    ## /etc/passwd 含有 root 的行都会列出来
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# 


三、grep 命令的常用参数。
①、--color=auto:对匹配的字符串用高光显示。
②、-v:排除过滤条件的行,也就是显示排除条件以外的内容。
③、-i:忽略大小写。
④、-n:显示匹配的行号,列出内容的同时也列出行号。
⑤、-c:统计匹配的行数,只列出共多少行符合过略条件,不列出内容。
⑥、-o:仅显示匹配的字符串,只显示文本中含过滤条件的字符串。
⑦、-q:静默模式,不输出任何信息。用于返回值判断,不考虑输出内容。
⑧、-A:(after),显示包含当前字符串的后多少行。
⑨、-B:(before),显示包含当前字符串的前多少行。
⑩、-C:(context),显示包含当前字符串的前后多少行。
⑩-①、-e:or,或。用于多个参数间的逻辑 或 判断。
⑩-②、-w:精确匹配,匹配整个单词。
⑩-③、-f:把过滤条件放到文件中,通过读取文件的过滤条件进行过滤。
⑩-④、-E:使用 egrep。(支持扩展正则表达式)
⑩-⑤、-F:使用 fgrep(不支持正则表达式)。


[root@localhost ~]# alias grep    ## 列出 grep 别名
alias grep='grep --color=auto'
[root@localhost ~]# 

[root@localhost ~]# vim .bashrc    ## 编辑 root 家目录下的 .bashrc 文件

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias grep='grep --color=auto'    ## <---- 添加别名并输入 --color=auto 参数
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

~                                                                     
~                                                                                       
~                                                                                       
:wq    ## 保存退出     

[root@localhost ~]# source ~/.bashrc    ## .bashrc 文件生效
[root@localhost ~]#                                 

②、-v:排除过滤条件的行,也就是显示排除条件以外的内容。

[root@localhost ~]# grep -v "sbin" /etc/passwd
root:x:0:0:root:/root:/bin/bash
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]# 
[root@localhost ~]# 

③、-i:忽略大小写。

## linux 大小写敏感,“Root” 和 “root” 是不一样的字符串。“Root” 不会有显示
[root@localhost ~]# grep "Root" /etc/passwd    
[root@localhost ~]# 

 ## -i 可以忽略大小写,只要是 root 这 4个字母都可以搜索出来
[root@localhost ~]# grep -i "Root" /etc/passwd   
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# 

④、-n:显示匹配的行号,列出内容的同时也列出行号。

[root@localhost ~]# 
[root@localhost ~]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash      ## 列出行号
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# 

## grep -n 和 cat -n 效果相似。(cat -n 输出结果后,管道 grep 过略 root 这个字符串)
[root@localhost ~]# cat -n /etc/passwd | grep "root"
     1  root:x:0:0:root:/root:/bin/bash
    10  operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# 

⑤、-c:统计匹配的行数,只列出共多少行符合过略条件,不列出内容。

[root@localhost ~]# 
[root@localhost ~]# grep -c "root" /etc/passwd    ## 含有 root 字符串的有 2 行
2

## grep -c 等价于 wc -l
[root@localhost ~]# grep "root" /etc/passwd | wc -l
2
[root@localhost ~]# 

⑥、-o:仅显示匹配的字符串,只显示文本中含过滤条件的字符串。

[root@localhost ~]# grep -o "root" /etc/passwd
root
root
root
root
[root@localhost ~]# 

⑦、-q:静默模式,文本中包不包含过滤条件的字符串都不输出任何信息。用于返回值判断,命令执行成功返回 0,失败返回非 0。

## /etc/passwd 有 root 字符串,不会输出任何信息。
[root@localhost ~]# grep -q "root" /etc/passwd    
[root@localhost ~]# 
[root@localhost ~]# echo $?    ## 通过打印 $? 返回值是 0,命令执行成功,也就是包含 root
0

## /etc/passwd 没有 bbbbbb 字符串,也不会输出任何信息。
[root@localhost ~]# grep -q "bbbbbb" /etc/passwd
[root@localhost ~]# 
[root@localhost ~]# echo $?     ## 通过打印 $? 返回值非 0,命令执行失败,也就是没有包含 bbbbbb
1
[root@localhost ~]# 

## grep 过滤 /etc/passwd 文件中的 root 字符串,正确输出和错误输出都重定向到垃圾桶
## 执行这样的命令没有信息返回,只有执行成功与否的返回值。
[root@localhost ~]# grep "root" /etc/passwd &> /dev/null 
[root@localhost ~]# 
[root@localhost ~]# echo $?    ## /etc/passwd 中有 root 返回 0
0
[root@localhost ~]# 
[root@localhost ~]# grep "zzzzzz" /etc/passwd &> /dev/null 
[root@localhost ~]# 
[root@localhost ~]# echo $?    ## /etc/passwd 中没有 zzzzzz 返回非 0
1
[root@localhost ~]# 

[root@localhost ~]# 
[root@localhost ~]# ls cron.txt &> /dev/null 
[root@localhost ~]# 
[root@localhost ~]# echo $?    ## 当前目录有 cron.txt,返回 0
0
[root@localhost ~]# 
[root@localhost ~]# ls abc.txt &> /dev/null 
[root@localhost ~]# 
[root@localhost ~]# echo $?     ## 当前目录没有 abc.txt,返回 2(非 0)
2
[root@localhost ~]# 

⑧、-A:(after),显示包含当前字符串的后多少行。

[root@localhost ~]# grep -nA3 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash    ## 含 root 的第 1 行
2-bin:x:1:1:bin:/bin:/sbin/nologin    ## 第 1 行后的 3 行显示出来
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin    ## 含 root 的第 10 行
11-games:x:12:100:games:/usr/games:/sbin/nologin    ## 第 10 行后的 3 行显示出来
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
[root@localhost ~]# 

⑨、-B:(before),显示包含当前字符串的前多少行。

[root@localhost ~]# 
[root@localhost ~]# grep -nB3 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash    ## 当前第 1 行含 root,前面没有行可以显示
--
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown    ## 第 10 行前 3 行
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin    ## 第 10 行含 root
[root@localhost ~]# 

⑩、-C:(context),显示包含当前字符串的前后多少行。

[root@localhost ~]# 
[root@localhost ~]# grep -nC3 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash    ## 第 1 行含有 root,只能显示后 3 行
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
--
7-shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown    ## 第 10 行的前 3 行显示出来
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin    ## 第 10 行含有 root
11-games:x:12:100:games:/usr/games:/sbin/nologin    ## 第 10 行的后 3 行显示出来
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
[root@localhost ~]# 

⑩-①、-e:or,或。用于多个参数间的逻辑 或 判断。

[root@localhost ~]# 
[root@localhost ~]# grep -e "root" -e "bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash    ## 含有 root 或 bash
operator:x:11:0:operator:/root:/sbin/nologin     ## 含有 root
torres:x:1000:1007::/home/torres:/bin/bash     ## 含有 bash
user1:x:1001:1008::/home/user1:/bin/bash    ## 含有 bash
x:x:1002:1009::/home/x:/bin/bash    ## 含有 bash
[root@localhost ~]# 

[root@localhost ~]# grep -e "root" -e "bash" -e "nologin" /etc/passwd
root:x:0:0:root:/root:/bin/bash     ## 含有 root 或 bash
bin:x:1:1:bin:/bin:/sbin/nologin    ## 含有 nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin    ## 含有 nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin    ## 含有 nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin    ## 含有 nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin    ## 含有 nologin
operator:x:11:0:operator:/root:/sbin/nologin    ## 含有 root 或 nologin 
games:x:12:100:games:/usr/games:/sbin/nologin     ## 含有 nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin     ## 含有 nologin
nobody:x:99:99:Nobody:/:/sbin/nologin     ## 含有 nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin     ## 含有 nologin
dbus:x:81:81:System message bus:/:/sbin/nologin     ## 含有 nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin     ## 含有 nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin     ## 含有 nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin     ## 含有 nologin
torres:x:1000:1007::/home/torres:/bin/bash    ## 含有 bash
user1:x:1001:1008::/home/user1:/bin/bash    ## 含有 bash
x:x:1002:1009::/home/x:/bin/bash    ## 含有 bash
[root@localhost ~]# 

[root@localhost ~]# grep "root" /etc/passwd | grep "bash" 
root:x:0:0:root:/root:/bin/bash

⑩-②、-w:精确匹配,匹配整个单词。

[root@localhost ~]# 
[root@localhost ~]# grep "bin" /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# grep -w "bin" /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]# 

[root@localhost ~]# grep "\bbin\b" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]# 

[root@localhost ~]# grep "\<bin\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]# 

[root@localhost ~]# echo "a abc b" | grep -w "abc"
a abc b
[root@localhost ~]# echo "a-abc-b" | grep -w "abc"
a-abc-b
[root@localhost ~]# echo "a,abc,b" | grep -w "abc"
a,abc,b
[root@localhost ~]# echo "a/abc/b" | grep -w "abc"
a/abc/b
[root@localhost ~]# echo "a\abc\b" | grep -w "abc"
a\abc\b
[root@localhost ~]# echo "a'abc'b" | grep -w "abc"
a'abc'b
[root@localhost ~]# echo "a~abc~b" | grep -w "abc"
a~abc~b
[root@localhost ~]# echo "a@abc@b" | grep -w "abc"
a@abc@b
[root@localhost ~]# echo "a%abc%b" | grep -w "abc"
a%abc%b
[root@localhost ~]# echo "a*abc*b" | grep -w "abc"
a*abc*b
[root@localhost ~]# echo "a(abc)b" | grep -w "abc"
a(abc)b
[root@localhost ~]# echo "a+abc+b" | grep -w "abc"
a+abc+b
[root@localhost ~]# echo "a>abc>b" | grep -w "abc"
a>abc>b
[root@localhost ~]# echo "a<abc<b" | grep -w "abc"
a<abc<b
[root@localhost ~]# echo "a>abc<b" | grep -w "abc"
a>abc<b
[root@localhost ~]# echo "a?abc?b" | grep -w "abc"
a?abc?b

[root@localhost ~]# echo "a_abc_b" | grep -w "abc"
[root@localhost ~]# echo $?    ## 返回值非 0 ,命令执行失败
1
[root@localhost ~]# echo "a1abc2b" | grep -w "abc"
[root@localhost ~]# echo $?
1
[root@localhost ~]# echo "a$abc$b" | grep -w "abc"
[root@localhost ~]# echo $?
1
[root@localhost ~]# echo "a`abc`b" | grep -w "abc"
-bash: abc: command not found
[root@localhost ~]# 

⑩-③、-f:把过滤条件放到文件中,通过读取文件的过滤条件进行过滤。

[root@localhost ~]# 
[root@localhost ~]# vim grepTest.txt    ##  编辑 grepTest.txt,存储过滤条件

root
bash
                                                                     
~                                                                                       
~                                                                                       
~                                                                                       
:wq        

[root@localhost ~]# 
[root@localhost ~]# grep -f grepTest.txt /etc/passwd    ## -f 读取 grepTest.txt 过滤条件
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
torres:x:1000:1007::/home/torres:/bin/bash
user1:x:1001:1008::/home/user1:/bin/bash
x:x:1002:1009::/home/x:/bin/bash
[root@localhost ~]# 
                  
上一篇下一篇

猜你喜欢

热点阅读