find
grep/egrep -i -n -E -v -o -l
sed
awk
1. sed命令查找⭐⭐⭐⭐⭐
1.1 根据行号进行过滤
# -n 取消sed命令的默认输出
# p输出print
[root@Kylin-V10-sp3 ~/test]# sed -n '3p' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@Kylin-V10-sp3 ~/test]#
1.2 根据行号范围进行过滤
#从第1行开始到第3行结束
[root@Kylin-V10-sp3 ~/test]# sed -n '1,3p' /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
[root@Kylin-V10-sp3 ~/test]#
#从第20行到最后一行
[root@Kylin-V10-sp3 ~/test]# sed -n '20,$p' /etc/passwd
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
dbus:x:81:81:D-Bus:/var/run/dbus:/sbin/nologin
polkitd:x:996:993:User for polkitd:/:/sbin/nologin
#文件最后一行
[root@Kylin-V10-sp3 ~/test]# sed -n '$p' /etc/passwd
nginx:x:990:986:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@Kylin-V10-sp3 ~/test]#
1.3 类似于grep/egrep进行过滤
#过滤指定内容 //
[root@Kylin-V10-sp3 ~/test]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@Kylin-V10-sp3 ~/test]#
#使用正则 记得加上-r
[root@Kylin-V10-sp3 ~/test]# sed -nr '/root|xk/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@Kylin-V10-sp3 ~/test]#
1.4 取出文件中某个范围的内容
# seq 3 25 >num.txt
# 从包含5的行到包含15的行
[root@Kylin-V10-sp3 ~/test]# seq 3 25 >num.txt
[root@Kylin-V10-sp3 ~/test]#
# 多个25,/5/匹配配到了25中5,找不到包含15的数字就会一直找到文件最后。
[root@Kylin-V10-sp3 ~/test]# sed -n '/5/ , /15/p' num.txt
5
6
7
8
9
10
11
12
13
14
15
25
[root@Kylin-V10-sp3 ~/test]# sed -n '/^5/ , /^15/p' num.txt
5
6
7
8
9
10
11
12
13
14
15
[root@Kylin-V10-sp3 ~/test]#
日志处理案例access.log过滤出 11点00分开始到11:10分结束的日志
温馨提示: