1 常见文本处理工具
cat
- -E:显示行结束符$
- -A:显示全部控制符
- -n:对显示出的每一行进行编号
- -b:非空行编号
- -s:压缩连续的空行成一行
[root@localhost ~]# cat a -A $ localhost.localdomain$ a b c$ a ^I^I d$ [root@localhost ~]# cat a -E $ localhost.localdomain$ a b c$ a d$
[root@localhost ~]# cat a -n
1
2 localhost.localdomain
3 a b c
4 a d
[root@localhost ~]# cat a -b
nginx
1 localhost.localdomain 2 a b c 3 a d 4 c
### nl 至关于cat -b
[root@localhost ~]# nl b
1 a
2 b
3 c
4 b
5 e
6 f
shell
### tac 逆向显示文本内容
[root@localhost ~]# tac b
f
e
b
c
b
a
centos
### rev 将同一行的内容逆向显示
[root@localhost ~]# echo {1..10} | rev
01 9 8 7 6 5 4 3 2 1
bash
### hexdump 查看非文本文件内容
[root@localhost ~]# hexdump /dev/sda -c -n 512less
### od od 即 dump fifiles in octal and other formats
[root@localhost ~]# 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
dom
echo {a..z} | tr -d ' '|xxd
0000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
0000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
ssh
### more 能够实现分页查看文件,能够配合管道实现输出信息的分页 ### less less 命令是man命令使用的分页器 ### head 能够显示文件或标准输入的前面行 * -c # 指定获取前#字节 * -n # 指定获取前#行 * -# 同上 ### tail tail 和head 相反,查看文件或标准输入的倒数行 * -c # 指定获取后#字节 * -n # 指定获取后#行 * -# 同上 * -f 跟踪显示文件fd新追加的内容,经常使用日志监控,至关于 --follow=descriptor,当文件删除再新建同名 * 文件,将没法继续跟踪文件 * -F 跟踪文件名,至关于--follow=name --retry,当文件删除再新建同名文件,将能够继续跟踪文件
tail -f /var/log/messages ###跟踪
tail -fn0 /var/log/messages ### 只看最新发生的日志
ide
### cut * -d DELIMITER: 指明分隔符,默认tab * -f FILEDS: #: 第#个字段,例如:3 #,#[,#]:离散的多个字段,例如:1,3,6 #-#:连续的多个字段, 例如:1-6 混合使用:1-3,7 * -c 按字符切割 * --output-delimiter=STRING指定输出分隔符
cut -d: -f1,3-5,7 /etc/passwd
[root@localhost ~]# echo {1..10} | cut -d' ' -f1,3,5-10
1 3 5 6 7 8 9 10
工具
[root@localhost ~]# ifconfig | head -n2 | tail -n1 | cut -d" " -f10
10.0.0.204
spa
[root@localhost ~]# ifconfig | head -n2 | tail -n1 | tr -s " " | cut -d " " -f3
10.0.0.204
[root@localhost ~]# df | tr -s " " | cut -d' ' -f5 | tr -dc "[0-9\n]"
00
2
0
2
1
14
0
df | tr -s ' ' % |cut -d% -f5 |tr -d '[:alpha:]'
df | cut -c44-46 |tr -d '[:alpha:]'
[root@centos8 ~]#cut -d: -f1,3,7 --output-delimiter="---" /etc/passwd
root---0---/bin/bash
bin---1---/sbin/nologin
daemon---2---/sbin/nologin
### paste paste 合并多个文件同行号的列到一行 * -d 分隔符:指定分隔符,默认用TAB * -s : 全部行合成一行显示
paste -d":" alpha.log seq.log
paste -s seq.log
paste -s alpha.log seq.log
### wc #文本数据统计 * -l 只计数行数 * -w 只计数单词总数 * -c 只计数字节总数 * -m 只计数字符总数 * -L 显示文件中最长行的长度
[root@localhost ~]# wc a
6 7 49 a
[root@localhost ~]# wc -l a
6 a
[root@localhost ~]# cat a | wc -l
6
### sort #整理文本 * -r 执行反方向(由上至下)整理 * -R 随机排序 * -n 执行按数字大小整理 * -f 选项忽略(fold)字符串中的字符大小写 * -u 选项(独特,unique),合并重复项,即去重 * -t c 选项使用c作为字段界定符 * -k # 选项按照使用c字符分隔的 # 列来整理可以使用屡次
[root@localhost ~]# cut -d: -f1,3 /etc/passwd | sort -t: -k2 -nr | head -n3
def:1001
abc:1000
polkitd:999
[root@localhost ~]# cut -d" " -f1 /var/log/nginx/access_log |sort -u|wc -l
cut: /var/log/nginx/access_log: No such file or directory
0
[root@centos8 ~]#df| tr -s ' ' '%'|cut -d% -f5|sort -nr|head -1
100
### uniq 去重 uniq命令从输入中删除先后相接的重复的行 * -c: 显示每行重复出现的次数 * -d: 仅显示重复过的行 * -u: 仅显示未曾重复的行
[root@localhost ~]# uniq a1 -c
2 aaaa
2 bbbb
1 ccc
[root@localhost ~]# uniq a1 -d
aaaa
bbbb
[root@localhost ~]# uniq a1 -u
ccc
[root@localhost ~]# sort a1 | uniq -c
2 aaaa
2 bbbb
1 ccc
2 ddd
[root@localhost ~]# ss -nt |tail -n+2 | tr -s ' '| cut -d" " -f4 | cut -d: -f1 | uniq -c
2 10.0.0.204
cut -d" " -f1 access_log |sort |uniq -c|sort -nr |head -3
lastb -f btmp-34 | tr -s ' ' |cut -d ' ' -f3|sort |uniq -c
|sort -nr | head -3
### diff和patch diffff 命令比较两个文件之间的区别 ### patch 复制在其它文件中进行的改变**(要谨慎使用)** 适用 -b 选项来自动备份改变了的文件 ### cmp
[root@localhost ~]# cmp /usr/bin/dir /usr/bin/ls
/usr/bin/dir /usr/bin/ls differ: byte 645, line 1
# 练习 一、找出ifconfifig “网卡名” 命令结果中本机的IPv4地址
[root@localhost ~]# ifconfig | head -n2 | tail -n1 | tr -s ' '| cut -d' ' -f3
10.0.0.204
二、查出分区空间使用率的最大百分比值
[root@localhost ~]# df | tr -s ' ' | tr -d % | cut -d' ' -f5 | tr -d '[:alpha:]' | sort -nr | head -n1
14
三、查出用户UID最大值的用户名、UID及shell类型
cut -d: -f3,1,7 /etc/passwd | sort -t: -nr -k2
四、查出/tmp的权限,以数字方式显示
[root@localhost ~]# stat /tmp | head -n4 | tail -n1 | cut -d: -f2 | cut -d/ -f1 | cut -d'(' -f2
1777
五、统计当前链接本机的每一个远程主机IP的链接数,并按从大到小排序
[root@localhost ~]# netstat -t | grep "ssh " | tr -s " " | cut -d' ' -f5 | cut -d: -f1 | uniq -c | sort -nr2 10.0.0.1