awk文本工具
1、文本处理工具grep
Linux
中最重要的三个命令在业界被称为“三剑客”,它们是awk,sed,grep
。
https://blog.csdn.net/sj349781478/article/details/82930982
1.1、grep[过滤出行]
grep
是行过滤工具,用于根据关键字进行行过滤。
语法:grep 【可选项】 ‘关键字’ 文件名
常见选项:
-i 忽略字符大小写
-v 查找不包含指定内容的行,反向选择
-w 按单词搜索
-o 打印匹配的关键字
-c 统计匹配到的次数
-n 显示行号
-r 逐层遍历目录查找
-A 显示匹配行以及后面多少行
-B: 显示匹配行及前面多少行
-C: 显示匹配行前后多少行
-l:只列出匹配的文件名
-L:列出不匹配的文件名
-e: 使用正则匹配
-E:使用扩展正则匹配
^key:以关键字开头
keyKaTeX parse error: Expected group after '^' at position 42: …d group after '^̲' at position 9…:匹配空行
--color=auto
:可以将找到的关键词部分加上颜色的显示
1.2、grep使用
1、高亮:grep --color=auto 'error' adapter.log
临时设置:alias grep='grep --color=auto'
永久高亮:
vi /etc/bashrc
最后添加:alias grep='grep --color=auto'
source /etc/bashrc
设置完后,这样也能高亮:grep 'error' adapter.log
2、过滤行并且显示行号
过滤出包含root
字符串的行并且显示行号
[root@localhost ~]# grep -n 'root' test
3:2020-10-28 21:21:37.887 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=/root/canal/adapter/bin
3、过滤出包含root
字符串的行忽略大小写,并且显示行号
[root@localhost ~]# grep -ni 'root' test
2:2020-10-28 21:22:18.370 ROOT [main-SendThread(localhost:2181)] WARN org.apacheEE.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
3:2020-10-28 21:21:37.887 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=/root/canal/adapter/bina
4、过滤出文件中以root
开头的行
[root@localhost ~]# grep '^root' test
root2020-10-28 21:22:18.370 [main-SendThread(localhost:2181)] WARN org.apache.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
5、过滤出文件中以xxx
结尾的行
[root@localhost ~]# grep 'reconnect$' test
root2020-10-28 21:22:18.370 [main-SendThread(localhost:2181)] WARN org.apache.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
2020-10-28 21:22:18.370 ROOT [main-SendThread(localhost:2181)] WARN org.apacheEE.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
6、过滤出文件中不以xxx
开头的行
使用-v
取反,即可
[root@localhost ~]# grep -niv '^root' test
2:2020-10-28 21:22:18.370 ROOT [main-SendThread(localhost:2181)] WARN org.apacheEE.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
3:2020-10-28 21:21:37.887 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=/root/canal/adapter/bina
7、过滤出文件中以xxx
开头的前x行,后x行
-B x 过滤出关键字的前几行
[root@localhost ~]# grep -n -B 1 '^ftp' test
3-2020-10-28 21:21:37.887 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=/root/canal/adapter/bina
4:ftp2020-10-28 21:21:37.887 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=/root/canal/adapter/bina
-A x 过滤出关键字的后几行
-C x 过滤出关键字的前后几行
8、根据单词去过滤出文件中的行
-w
根据单词去过滤行。
[root@localhost ~]# grep -w 'hello' test
hello world
helloworld
是过滤出不出来的。
9、统计文件中某个单词出现的次数
[root@localhost ~]# grep -o "a" test | wc -l
30
-o
打印匹配的关键字
|
将结果输出给下一个命令
wc
将计算指定文件的行数、字数,以及字节数。
-l
只显示行数。
10、在大日志文件中搜索关键字,最后/最开始出现位置
1、先查出关键字,前后一行的数据:grep -C 1 ‘error’ test
2、然后将结果通过|
,输出给后面的tail
命令去统计:grep -C 1 'error' test | tail -2
tail -2 要显示文件的最后2行
1.3、cut【截取列】
grep
是行的过滤,cut
是列的截取
语法:cut 选项 文件名
-c: 以字符为单位进行分割,截取
-d: 自定义分隔符,默认为制表符\t
-f: 与-d一起使用,指定截取哪个区域
1、截取:
号的第一列
-d:表示使用冒号分割
-f1 表示取分割的第一列
[root@localhost ~]# cut -d: -f1 test
aaaaaaaaaaaaaaaaaaaaaaa
root2020-10-28 21
2020-10-28 21
2020-10-28 21
ftp2020-10-28 21
hello world
helloworld
2、截取:
号的第一列和第七列
[root@localhost ~]# cut -d: -f1,7 test
aaaaaaaaaaaaaaaaaaaaaaa
root2020-10-28 21: Session
2020-10-28 21
2020-10-28 21
ftp2020-10-28 21
hello world
helloworld
3、截取从第一个字符到第五个字符
[root@localhost ~]# cut -c1-5 test
aaaaa
root2
2020-
2020-
ftp20
hello
hello
-c10-
:表示从第十个字符开始全部截取
注意:列的截取,使用变量的时候也可以截取
#第8位开始,截取4位,索引从0开始
${luck_num:7:4}
1.4、练习1
如何过滤出运行级别?
1、获取运行级别的方法
runlevel
[root@localhost ~]# cat /etc/inittab
2、解决:
使用cut
[root@localhost ~]# runlevel
N 3
[root@localhost ~]# runlevel | cut -d ' ' -f2
3
[root@localhost ~]# runlevel | cut -c3
3
使用grep
:
[root@localhost ~]# grep '^id' /etc/inittab | cut -c4
3
[root@localhost ~]# grep -v '^#' /etc/inittab | cut -