文本处理工具
查看文本文件内容
cat:可以查看文本内容
格式:
cat [OPTION]... [FILE]...
常见选项:
-E:显示行结束符$
-A:显示所有控制符
-n:对显示出的每一行进行编号
-b:非空行编号
-s:压缩连续的空行成一行
nl:显示行号,相当于cat -b
范例:
[root@CentOS8 ~]# seq 5 > 1.txt
[root@CentOS8 ~]# cat -n 1.txt
1
2
3
4 1
5 2
6 3
7 4
8 5
[root@CentOS8 ~]# cat -b 1.txt
1 1
2 2
3 3
4 4
5 5
[root@CentOS8 ~]# nl 1.txt
1 1
2 2
3 3
4 4
5 5
tac:逆向显示文本内容
[root@CentOS8 ~]# cat 1.txt
1
2
3
4
5
[root@CentOS8 ~]# tac 1.txt
5
4
3
2
1
[root@CentOS8 ~]# seq 10 | tac
10
9
8
7
6
5
4
3
2
1
rev:将同一行的内容逆向显示
[root@CentOS8 ~]# cat 1.txt
1 2 3 4 5
a b c
[root@CentOS8 ~]# tac 1.txt
a b c
1 2 3 4 5
[root@CentOS8 ~]# rev 1.txt
5 4 3 2 1
c b a
[root@CentOS8 ~]# echo {1..10} | rev
01 9 8 7 6 5 4 3 2 1
查看非文本文件内容
hexdump命令
hexdump是一个二进制文件查看工具,它可以将二进制文件转换为ASCII、八进制、十进制、十六进制格式进行查看。
[root@CentOS8 ~]# hexdump -C -n 512 /dev/sda
00000000 eb 63 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.c..............|
[root@CentOS8 ~]# hexdump -C
abc
00000000 61 62 63 0a |abc.|
00000004
[root@CentOS8 ~]# echo {a..z} |tr -d ' ' |hexdump -C
00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|
00000010 71 72 73 74 75 76 77 78 79 7a 0a |qrstuvwxyz.|
0000001b
od命令
od(octal dump)命令可以以八进制、十进制、十六进制和ASCII码的格式来显示文件或者流,它们对于访问或可视地检查文件中不能直接显示在终端上的字符(如换行符等)很有用。此外,在一些有空洞的文件中可以通过od命令查看空洞的位置。
[root@CentOS8 ~]# echo {a..z} |tr -d ' ' |od -t x
0000000 64636261 68676665 6c6b6a69 706f6e6d
0000020 74737271 78777675 000a7a79
0000033
[root@CentOS8 ~]# echo {a..z} |tr -d ' ' |od -t x1
0000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70
0000020 71 72 73 74 75 76 77 78 79 7a 0a
0000033
[root@CentOS8 ~]# echo {a..z} |tr -d ' ' |od -t x1z
0000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 >abcdefghijklmnop<
0000020 71 72 73 74 75 76 77 78 79 7a 0a >qrstuvwxyz.<
0000033
xxd命令
xxd命令主要用来查看文件对应的十六进制形式,也可以讲文件对应的十六进制形式输出到一个指定的文件。使用此命令所支持的特有选项,亦可以以二进制的形式查看文件。
[root@CentOS8 ~]# echo {a..z} |tr -d ' ' |xxd
00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop00000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
页查看文件内容
more命令
more:可以实现分页查看文件,可以配合管道实现输出信息的分页
格式
more [OPTIONS...] FILE...
选项:-d:显示翻页及退出提示
less命令
less:可以实现分页查看文件或STDIN输出,less命令时man命令使用的分页器
查看时有用的命令包括:/文本 向下搜索 文本/?文本 向上搜索
n/N 跳到下一个 或 上一个匹配
显示文本前后或后行内容
head命令
head:可以显示文本或标准输入的前面行
格式:
head [OPTION]... [FILE]...
选项:-c # 指定获取前#字节-n # 指定获取前#行-# 同上
[root@CentOS8 ~]# head -n 3 /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@CentOS8 ~]# head -3 /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@CentOS8 ~]# seq 10 > 1.txt
[root@CentOS8 ~]# head -n -3 1.txt
1
2
3
4
5
6
7
[root@CentOS8 ~]# echo abcdef | head -c 3
abc[root@CentOS8 ~]#
tail命令
tail:tail和head相反,查看文件或标准输入的倒数行
格式:
tail [OPTION]... [FILE]...
-c # 指定获取后#字节
-n # 指定获取后#行
-# 同上
-f 跟踪显示文件fd新追加的内容,常用日志监控,相当于--follow=descriptor,当文件删除再新建同名文件,无法继续跟踪文件
-F 跟踪文件名,相当于--follow=name --retry,当文件删除再新建同名文件,可以继续跟踪文件
[root@CentOS8 ~]# cat 1.txt
1
2
3
4
5
6
7
8
9
10
[root@CentOS8 ~]# tail -n3 1.txt
8
9
10
[root@CentOS8 ~]# tail -3 1.txt
8
9
10
[root@CentOS8 ~]# tail -n +3 1.txt
3
4
5
6
7
8
9
10
[root@CentOS8 ~]# seq 10 | head -6 | tail -1
6
[root@CentOS8 ~]# seq 10 | tail -n +6 | head -1
6
按列抽取文本
cut命令
cut:可以提取文本文件或STDIN数据的指定列
格式
cut [OPTION]... [FILE]...
选项
-d DELIMITER:指明分隔符:默认tab
-f FILEDS:
#:第#个字段,例如:3
#,#[,#]:离散的多个字段,例如:1,3,6
#-#:连续的多个字段,例如:1-6
混合使用:1-3,7
-c 按字符切割
--output-delimiter=STRING指定输出分隔符
范例:
[root@CentOS8 ~]# cut -d: -f1,3-4,7 /etc/passwd
[root@CentOS8 ~]# df | tr -s ' ' | cut -d ' ' -f 5 | tr -d %|tail -n +2
[root@centos8 ~]# df | tail -n +2|tr -s ' ' % |cut -d% -f5
合并多个文件
paste命令
paste:合并多个文件同行号的列到一行
格式
paste [OPTION]... [FILE]...
常用选项:-d:分隔符:指定分隔符,默认为TAB-S:所有行合成一行显示
[root@centos8 ~]#cat alpha.log
a
b
c
d
e
f
g
h
[root@centos8 ~]#cat seq.log
1
2
3
4
5
[root@centos8 ~]#cat alpha.log seq.log
a
b
c
d
e
f
g
h
1
2
3
4
5
[root@centos8 ~]#paste alpha.log seq.log
a 1
b 2
c 3
d 4
e 5
f
g
h
[root@centos8 ~]#paste -d":" alpha.log seq.log
a:1
b:2
c:3
d:4
e:5
f:
g:
h:
[root@centos8 ~]#paste -s seq.log
1 2 3 4 5
[root@centos8 ~]#paste -s alpha.log
a b c d e f g h
[root@centos8 ~]#paste -s alpha.log seq.log
a b c d e f g h
1 2 3 4 5
[root@centos8 ~]#cat title.txt
ceo
coo
cto
[root@centos8 ~]#cat emp.txt
mage
zhang
wang
xu
[root@centos8 ~]#paste title.txt emp.txt
ceo mage
coo zhang
cto wang
xu
[root@centos8 ~]#paste -s title.txt emp.txt
ceo coo cto
mage zhang wang xu
[root@centos8 ~]#seq 100|paste -d + -s|bc
5050
[root@centos8 ~]#cat alpha.log
a
b
c
d
e
f
g
h
[root@centos8 ~]#cat seq.log
1
2
3
4
5
[root@centos8 ~]#cat alpha.log seq.log
a
b
c
d
e
f
g
h
1
2
3
4
5
[root@centos8 ~]#paste alpha.log seq.log
a 1
b 2
c 3
d 4
e 5
f
g
h
[root@centos8 ~]#paste -d":" alpha.log seq.log
a:1
b:2
c:3
d:4
e:5
f:
g:
h:
[root@centos8 ~]#paste -s seq.log
1 2 3 4 5
[root@centos8 ~]#paste -s alpha.log
a b c d e f g h
[root@centos8 ~]#paste -s alpha.log seq.log
a b c d e f g h
1 2 3 4 5
[root@centos8 ~]#cat title.txt
ceo
coo
cto
[root@centos8 ~]#cat emp.txt
mage
zhang
wang
xu
[root@centos8 ~]#paste title.txt emp.txt
ceo mage
coo zhang
cto wang
xu
[root@centos8 ~]#paste -s title.txt emp.txt
ceo coo cto
mage zhang wang xu
[root@centos8 ~]#seq 100|paste -d + -s|bc
5050
分析文本工具
文本数据统计:wc
整理文本:sort
比较文件:diff和patch
收集文本统计数据wc
常用选项:
-l只统计行数
-w只计数单词总数
-c只计数字节总数
-m只计数字符总数
-L显示文件中最长行的长度
wc story.txt
39 237 1901 story.txt
行数 单词数 字节数
[root@CentOS8 ~]# df | tail -n $(echo `df | wc -l`-1 | bc)
devtmpfs 897700 0 897700 0% /dev
tmpfs 916500 0 916500 0% /dev/shm
tmpfs 916500 8916 907584 1% /run
tmpfs 916500 0 916500 0% /sys/fs/cgroup
/dev/sda2 104806400 3213588 101592812 4% /
/dev/sda3 52403200 398400 52004800 1% /data
/dev/sda1 1038336 189356 848980 19% /boot
tmpfs 183300 0 183300 0% /run/user/0
整理文本:sort
sort
把整理过的文本显示在STDOUT,不改变原始文件
格式:
sort [options] file(s)
常用选项
-r 执行反方向(由上至下)整理
-R 随机排序
-n 执行按数字大小整理
-f 选项忽略(fold)字符串中的字符大小写
-u 选项(独特,unique),合并重复项,即去重
-t c 选项使用c作为字段定界符
-k # 选项按照使用c字符分割的#列来整理能够使用多次
[root@CentOS8 ~]# cut -d: -f 1,3 /etc/passwd | sort -t: -k2 -nr | head -n3
nobody:65534
wang:1000
systemd-coredump:999
#查看分区利用率最高值
[root@centos8 ~]#df| tr -s ' ' '%'|cut -d% -f5|sort -nr|head -1
100
面试题:有两个文件,a.txt与b.txt ,合并两个文件,并输出时确保每个数字也唯一
[root@CentOS8 ~]# cat a.txt b.txt | sort -nr | uniq -u
23452345
1341345
1235345
54361
12345
12341
2345
100
[root@CentOS8 ~]# cat a.txt
200
100
23452345
1234
12341
1235345
[root@CentOS8 ~]# cat b.txt
1234
200
2345
54361
12345
1341345
去重uniq
uniq命令从输入中删除前后相接的重复的行
格式:
uniq [OPTION]... [FILE]...
常见选项
-c:显示每行重复出现的次数
-d:仅显示重复过的行
-u:仅显示不曾重复的行
unqi常和sort命令一起配合使用:例:sort userlist.txt | uniq -c
例:统计日志访问量最多的请求
[root@centos8 data]#cut -d" " -f1 access_log |sort |uniq -c|sort -nr |head -3
4870 172.20.116.228
3429 172.20.116.208
2834 172.20.0.222
[root@centos8 data]#lastb -f btmp-34 | tr -s ' ' |cut -d ' ' -f3|sort |uniq -c |sort -nr | head -3
86294 58.218.92.37
43148 58.218.92.26
18036 112.85.42.201
例:并发链接最多的远程主机IP
[root@centos8 ~]#ss -nt|tail -n+2 |tr -s ' ' : |cut -d: -f6|sort|uniq -c|sort -nr |head -n27 10.0.0.12 10.0.0.7
例:取两个文件的相同和不同的行
[root@centos8 data]#cat test1.txt
a
b
1
c
[root@centos8 data]#cat test2.txt
b
e
f
c
1
2
#取文件的共同行
[root@centos8 data]#cat test1.txt test2.txt | sort |uniq -d
1
b
c
#取文件的不同行
[root@centos8 data]#cat test1.txt test2.txt | sort |uniq -u
2
a
e
F
比较文件
diff命令比较两个文件之间的区别,diff命令的输出可被保存在一种叫做“补丁”的文件中
常用选项:-u选项来输出“统一的(unified)”diff格式文件,最适用于补丁文件
[root@centos8 ~]#cat f1.txt
mage
zhang
wang
xu
[root@centos8 ~]#cat f2.txt
magedu
zhang sir
wang
xu
shi
[root@centos8 ~]#diff f1.txt f2.txt
1,2c1,2
< mage
< zhang
---
> magedu
> zhang sir
4a5
> shi
[root@centos8 ~]#diff -u f1.txt f2.txt
--- f1.txt 2019-12-13 21:31:30.892775671 +0800
+++ f2.txt 2019-12-13 22:00:14.373677728 +0800
@@ -1,4 +1,5 @@
-mage
-zhang
+magedu
+zhang sir
wang
xu
+shi
[root@centos8 ~]#diff -u f1.txt f2.txt > f.patch
[root@centos8 ~]#rm -f f2.txt
[root@centos8 ~]#patch -b f1.txt f.patch
patching file f1.txt
[root@centos8 ~]#cat f1.txt
magedu
zhang sir
wang
xu
shi
[root@centos8 ~]#cat f1.txt.orig
mage
zhang
wang
xu
[root@centos8 ~]#cat f1.txt
mage
zhang
wang
xu
[root@centos8 ~]#cat f2.txt
magedu
zhang sir
wang
xu
shi
[root@centos8 ~]#diff f1.txt f2.txt
1,2c1,2
< mage
1.7.18 < zhang
---
> magedu
> zhang sir
4a5
> shi
[root@centos8 ~]#diff -u f1.txt f2.txt
--- f1.txt 2019-12-13 21:31:30.892775671 +0800
+++ f2.txt 2019-12-13 22:00:14.373677728 +0800
@@ -1,4 +1,5 @@
-mage
-zhang
+magedu
+zhang sir
wang
xu
+shi
[root@centos8 ~]#diff -u f1.txt f2.txt > f.patch
[root@centos8 ~]#rm -f f2.txt
[root@centos8 ~]#patch -b f1.txt f.patch
patching file f1.txt
[root@centos8 ~]#cat f1.txt
magedu
zhang sir
wang
xu
shi
[root@centos8 ~]#cat f1.txt.orig
mage
zhang
wang
xu