006. 文本处理工具 P2 (常见文本处理工具)

北城以北 提交于 2020-11-13 09:53:34

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




 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






### tac 
逆向显示文本内容

[root@localhost ~]# tac b
f
e
b
c
b
a






### rev
将同一行的内容逆向显示

[root@localhost ~]# echo {1..10} | rev
01 9 8 7 6 5 4 3 2 1


### hexdump
查看非文本文件内容

[root@localhost ~]# hexdump /dev/sda -c -n 512


### 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


echo {a..z} | tr -d ' '|xxd
0000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
0000010: 7172 7374 7576 7778 797a 0a             qrstuvwxyz.

### 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 ### 只看最新发生的日志

### 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

[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


# 练习
1、找出ifconfifig “网卡名” 命令结果中本机的IPv4地址

[root@localhost ~]# ifconfig | head -n2 | tail -n1 | tr -s ' '| cut -d' ' -f3
10.0.0.204

2、查出分区空间使用率的最大百分比值

[root@localhost ~]# df | tr -s ' ' | tr -d % | cut -d' ' -f5 | tr -d '[:alpha:]' | sort -nr | head -n1
14

3、查出用户UID最大值的用户名、UID及shell类型

cut -d: -f3,1,7 /etc/passwd | sort -t: -nr -k2

4、查出/tmp的权限,以数字方式显示

[root@localhost ~]# stat /tmp | head -n4 | tail -n1 | cut -d: -f2 | cut -d/ -f1 | cut -d'(' -f2
1777

5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@localhost ~]# netstat -t | grep "ssh " | tr -s " " | cut -d' ' -f5 | cut -d: -f1 | uniq -c | sort -nr
2 10.0.0.1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!