2018-03-12阿铭Linux学习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-03-12阿铭Linux学习相关的知识,希望对你有一定的参考价值。
9.1 正则介绍 grep (上)
什么是正则
正则就是一串有规律的字符串
掌握好正则对于编写shell脚本有很大帮助
各种编程语言中都有正则,原理是一样的
本章主要学习 grep/egrep、sed、awk
掌握规律
grep
grep [-cinvABC] ‘work‘ filename
-c 行数
-i 不区分大小写
-n 显示行号
-v 取反
-r 遍及所有子目录
-A 后面跟数字,过滤出符合要求的行以及下面n行
-B 后面跟数字,过滤出符合要求的行以及上面n行
-C 后面跟数字,同时过滤出符合要求的行以及上下各n行
[[email protected] grep]# grep -c ‘nologin‘ passwd
16
[[email protected] grep]# grep -n ‘nologin‘ passwd
[[email protected] grep]# grep -i -c ‘nologin‘ passwd
16
[[email protected] grep]# grep -v -c ‘nologin‘ passwd
4
[[email protected] grep]# grep -vc ‘nologin‘ passwd
4
[[email protected] grep]# grep -v ‘nologin‘ passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[[email protected] grep]# grep -A2 ‘root‘ passwd
9.2 grep (中)
grep/egrep 示例
grep -n ‘root‘ /etc/passwd
grep -nv ‘nologin‘ /etc/passwd
grep ‘[0-9]‘ /etc/ininttab
grep -v ‘[0-9]‘ /etc/inittab
grep -v ‘^#‘ inittab
grep -v ‘^#‘ inittab|grep -v ‘^$‘
grep ‘^[^a-zA-Z]‘ test.txt
grep ‘r.o‘ test.txt
grep ‘oo‘ test.txt
grep ‘.*‘ test.txt
grep ‘o\{2\}‘ /etc/passwd
egrep ‘o{2}‘ /etc/passwd
egrep ‘o+‘ /etc/passwd
egrep ‘oo?‘ /etc/passwd
egrep ‘root|nologin‘ /etc/passwd
egrep ‘(oo){2}‘ /etc/passwd
[[email protected] grep]# grep -n ‘^#‘ 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:
16:# systemctl get-default
17:#
18:# To set a default target, run:
19:# systemctl set-default TARGET.target
20:#
[[email protected] grep]# grep -nv ‘^#‘ inittab
13:adfafsafd
14:ssss
15:66666
[[email protected] grep]# grep -n ‘[^0-9]‘ 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 maintargets:
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:adfafsafd
14:ssss
16:# systemctl get-default
17:#
18:# To set a default target, run:
19:# systemctl set-default TARGET.target
20:#
[[email protected] grep]# grep -n ‘^[^0-9]‘ 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:adfafsafd
14:ssss
16:# systemctl get-default
17:#
18:# To set a default target, run:
19:# systemctl set-default TARGET.target
20:#
[[email protected] grep]# grep -nv ‘^[^0-9]‘ inittab
15:66666
9.3 grep (下)
. 匹配.两边的字符中任意一个
* 重复*左边的字符0或无数个
+ 重复+左边的字符1到无数个
? 重复?前面字符的0或1次
| 或者
() 找出群组字串
()+ 多个重复群组字串的判别
[[email protected] grep]# grep ‘r.o‘ passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] grep]# grep ‘{2}‘ passwd
[[email protected] grep]# grep ‘o{2}‘ passwd
[[email protected] grep]# grep ‘o\{2\}‘ passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
bacula:x:133:133:Bacula Backup System:/var/spool/bacula:/sbin/nologin
grep -E ‘o{2}‘ passwd === egrep ‘o{2}‘ passwd
[[email protected] grep]# egrep ‘o+o‘ passwd
[[email protected] grep]# grep -E ‘root|nologin‘ passwd
[[email protected] data]# grep -r --include="*.php" ‘eval‘ /tmp/data
/tmp/data/ext.php:eval
以上是关于2018-03-12阿铭Linux学习的主要内容,如果未能解决你的问题,请参考以下文章