linux文本处理工具

文本内容查看工具

查看文本文件内容

cat ...
-E:显示行结束符$
-A:显示所有控制符
-n:对显示出的每一行进行编号
-b:非空行编号
-s:压缩连续的空行成一行

nl ...
显示行号,相当于cat -b
[root@localhost ~]# nl a.txt 
     1	a
       
     2	b
       
     3	c

tac ...
逆向显示文本内容
[root@localhost ~]# tac a.txt 
c

b

a

rev ...
将同一行的内容逆向显示
[root@localhost ~]# cat b.txt 
1 2 3 4 5
abcdef
[root@localhost ~]# rev b.txt 
5 4 3 2 1
fedcba

查看非文本文件内容 hexdump

hexdump是Linux下的一个二进制文件查看工具,它可以将二进制文件转换为ASCII、八进制、十进制、十六进制格式进行查看。
(注意:在Linux中换行符\n 的十六进制为0a,在windows中,换行为\r\n的十六进制编码为:0d 0a
[root@localhost ~]# cat test.txt 
abcdefghijklmnopqrstuvwxyz
-C 输出规范的十六进制和ASCII码 
[root@localhost ~]# hexdump -C test.txt 
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
-s 从偏移量开始格式输出。若指定参数-s 5,则前面的abcde字符就没有了。
[root@localhost ~]# hexdump -s 5 -C test.txt 
00000005  66 67 68 69 6a 6b 6c 6d  6e 6f 70 71 72 73 74 75  |fghijklmnopqrstu|
00000015  76 77 78 79 7a 0a                                 |vwxyz.|
0000001b
-n 只格式前n个长度的字符
[root@localhost ~]# hexdump -n 5 -C test.txt 
00000000  61 62 63 64 65                                    |abcde|
00000005
-c 每个字节显示为ASCII字符
[root@localhost ~]# hexdump  -c test.txt 
0000000   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p
0000010   q   r   s   t   u   v   w   x   y   z  \n                    
000001b
-v 显示时不压缩相似的行

分页查看文件内容

more
可以实现分页查看文件,可以配合管道实现输出信息的分页
less
也可以实现分页查看文件

显示文本前或后行内容

head

-c x 指定获取前x字节 注:一个汉字为3个字节
-n x 指定获取前x行,x如果为负数,表示从文件头取到倒数第x行前

输出前25行
head -n25 input.txt
head -n 25 input.txt
head -25 input.txt
输出前5个字符
head -c5 test.cpp
head -c 5 test.cpp

[root@localhost ~]# cat /etc/passwd | head -n -3  表示从文件头取到倒数第3行前
[root@localhost ~]# echo 爱ab | head -c 4
爱a

tail

tail 和head 相反,查看文件或标准输入的倒数行
-c x 指定获取后x字节
-n x 指定获取后x行,如果x是正数,表示从第x行开始取到文件结束
-f   跟踪显示文件新追加的内容,常用日志监控

按列抽取文本cut

cut 命令可以提取文本文件
-d DELIMITER: 指明分隔符
-f FILEDS:
3: 第3个字段,例如:3
x,y,z:离散的多个字段,例如:1,3,5
x-y:连续的多个字段, 例如:1-3
混合使用:1-3,7
-c 按字符切割
实例:
取出分区利用率:
[root@localhost ~]# df | tail -n +2 | tr -s ' '| cut -d ' ' -f5 | tr -d %
[root@localhost ~]# df | tail -n +2 | tr -s ' '| cut -d ' ' -f5 | cut -d % -f1
[root@localhost ~]# df | tail -n +2 | tr -s ' ' % | cut -d% -f5

合并多个文件paste

paste 合并多个文件,同行号的列到一行
-d 分隔符:指定分隔符
-s 所有行合成一行显示
[root@localhost ~]# cat user.txt 
gun1
gun2
gun3
[root@localhost ~]# cat passwd.txt 
centos1
centos2
centos3
[root@localhost ~]# paste -d: user.txt passwd.txt 
gun1:centos1
gun2:centos2
gun3:centos3
[root@localhost ~]# paste -s user.txt passwd.txt 
gun1	gun2	gun3
centos1	centos2	centos3
[root@localhost ~]# seq 10 | paste -d+ -s
1+2+3+4+5+6+7+8+9+10
[root@localhost ~]# seq 10 | paste -d+ -s|bc
55

文本内容分析工具

wc

可用于统计文件的行总数、单词总数、字节总数和字符总数
可以对文件或STDIN中的数据统计
-l 只计行数
-w 只计单词总数
-c 只计字节总数
-m 只计字符总数
-L 显示文件中最长行的长度
[root@localhost ~]# wc user.txt 
 3  3 15 user.txt
行数  单词数  字节数 
[root@localhost ~]# cat user.txt | wc -l
3
统计文件个数
[root@localhost ~]# ls | wc -l
16

sort

文本排序
-r 倒序排列,默认正序
-R 随机排序
-t : 使用:作为字段分隔符
   :对于特殊符号(如制表符),可使用类似于-t $'\t' 或-t'ctrl+v,tab'(先按ctrl+v,然后按tab键)的方法实现。
-n 按数字大小整理
-h 可读排序,如: 2K 1G
-u 选项(独特,unique),合并重复项,即去重
-k 3 对分割字符的第3列进行排序
不加任何选项时,将对整行从第一个字符开始依次向后直到行尾按照默认的字符集排序规则做升序排序

[root@localhost ~]# sort -t: -k3 -nr /etc/passwd
取出分区利用率最高的
[root@localhost ~]# df | tail -n +2 | tr -s ' ' % | cut -d% -f5| sort -nr |head -n1
73
案例:
以第三列为排序列进行排序。由于要划分字段,所以指定字段分隔符。指定制表符这种无法直接输入的特殊字符的方式是$'\t'。

[root@linux]# sort -t $'\t' -k3 system.txt  
4       linux   1000    200
3       bsd     1000    600
1       mac     2000    500
2       winxp   4000    300
5       SUSE    4000    300
6       Debian  600     200
结果中虽然1000<2000<4000的顺序是对了,但600却排在最后面,因为这是按照默认字符集排序规则进行排序的,字符6大于4,所以排最后一行

uniq

uniq命令从输入中删除前后相接的重复的行
-c: 显示每行重复出现的次数
-d: 仅显示重复过的行
-u: 仅显示不曾重复的行

查看网页访问ip量
[root@localhost ~]# cut -d " " -f1 access_log | sort | uniq -c | sort -nr | head
   4870 172.20.116.228
   3429 172.20.116.208
   2834 172.20.0.222
   2613 172.20.112.14
   2267 172.20.0.227
   2262 172.20.116.179
   2259 172.20.65.65
   1565 172.20.0.76
   1482 172.20.0.200
   1110 172.20.28.145

找到两个文件重复的行
[root@localhost ~]# cat 1.txt 2.txt  | sort|uniq -d
a
b

diff

比较文件
diff 命令比较两个文件之间的区别
[root@localhost ~]# cat 1.txt 
a
a
b
b
c
[root@localhost ~]# cat 2.txt 
1
2
3
[root@localhost ~]# diff -u 1.txt 2.txt 
--- 1.txt	2021-10-25 19:10:13.253019232 +0800
+++ 2.txt	2021-10-25 19:10:24.538019422 +0800
@@ -1,5 +1,3 @@
-a
-a
-b
-b
-c
+1
+2
+3
posted @ 2021-10-22 21:56  咚咚小孩  阅读(74)  评论(0)    收藏  举报