欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

9.2-grep命令(中)

程序员文章站 2022-05-13 11:01:30
...

-n 'root' 过滤出带有某个关键词的行,并输出行号

[aaa@qq.com grep]# grep -n 'root' passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[aaa@qq.com grep]# 

-nv 'nologin' 过滤出不带有某个关键词的行,并输出行号

[aaa@qq.com grep]# grep -nv 'nologin' passwd
1:root:x:0:0:root:/root:/bin/bash
2:bin:x:1:1:bin:/bin:/sbin/NOLogin
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
20:evan1:x:1000:1000::/home/evan1:/bin/bash
21:evan2:x:1001:1001::/home/evan2:/bin/bash
22:user1:x:1002:1002::/home/user1:/bin/bash
23:user2:x:1004:1005::/home/user2:/bin/bash
25:user4:x:1007:1007::/home/user4:/bin/bash
[aaa@qq.com grep]#

-n '[0-9]' 过滤出所有包含数字的行,并输出行号

[aaa@qq.com grep]# grep -n '[0-9]' /etc/inittab
9:# multi-user.target: analogous to runlevel 3
10:# graphical.target: analogous to runlevel 5
[aaa@qq.com grep]#

-nv '[0-9]' 过滤出所有不包含数字的行,并输出行号

[aaa@qq.com grep]# grep -nv '[0-9]' /etc/inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
8:#
11:#
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#
[aaa@qq.com grep]# 

可以看到第9和第10行没有了

-n '^#' 过滤出所有以 # 开头的行,并输出行号
查看以 # 开头的行,并输出行号

[aaa@qq.com grep]# grep -n '^#' /etc/inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
8:#
9:# multi-user.target: analogous to runlevel 3
10:# graphical.target: analogous to runlevel 5
11:#
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#
[aaa@qq.com grep]#

我们发现这个文件所有行中都有 # ,接下来,我们 cp 一份这个文件,然后对它进行一下编辑

[aaa@qq.com grep]# cp /etc/inittab .
[aaa@qq.com grep]# ls
inittab  passwd
[aaa@qq.com grep]# vi inittab

手动添加一行,并保存退出。
9.2-grep命令(中)

[aaa@qq.com grep]# grep -n '^#' /etc/inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
8:#
9:# multi-user.target: analogous to runlevel 3
10:# graphical.target: analogous to runlevel 5
11:#
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#
[aaa@qq.com grep]#

我们发现,我们最后添加的那行是没有展示的

过滤掉以 # 开头的行,并输出行号

[aaa@qq.com grep]# grep -nv '^#' inittab
18:ahcbabvjjs3761738183
[aaa@qq.com grep]# 

过滤掉所有空行和以 # 开头的行

[aaa@qq.com grep]# grep -v '^#' inittab | grep -v '^$'
ahcbabvjjs3761738183
[aaa@qq.com grep]# 

[^0-9] 匹配非方括号里面的内容
[^] 匹配非方括号里面的内容。就是:只要不是方括号里面的内容都打印出来。
把非数字的内容呈现出来

[aaa@qq.com grep]# grep '[^0-9]' inittab

9.2-grep命令(中)符合条件的字符均已加粗,只有数字没变粗,表明没筛选没有符合条件

^[^0-9] 匹配非方括号里面的内容开头的所有字符
^[^] 匹配非方括号里面的内容开头的所有字符。 只要不是方括号里面的内容所开头的,都打印出来。
把非数字开头的内容呈现出来

[aaa@qq.com grep]# grep '^[^0-9]' inittab

9.2-grep命令(中)符合条件的字符均已加粗

相关标签: grep命令