文本截取工具

文本截取工具

cut [OPTION]... [FILE]...(按列抽取文本)

-d DELIMITER: 指明分隔符,默认tab
-f FILEDS:
#: 第#个字段
#,#[,#]:离散的多个字段,例如1,3,6
#-#:连续的多个字段, 例如1-6
混合使用:1-3,7
-c 按字符切割
--output-delimiter=STRING指定输出分隔符

示例一:

取出文本的第一列到第三列:

[root@ansibledata]#getent passwd | cut -d: -f1-3
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
games:x:12
ftp:x:14

示例二:

将df显示的文本按空格压缩,用cut以%作为分隔符,取第5列,将磁盘利用率取出来:

[root@ansibledata]#df | tr -s " " % |cut -d% -f5
Use
5
0
0
2
0
1
17
1

示例三:

将网卡的IP地址取出来。

[root@ansibledata]#ifconfig ens33 | tr -s " " | head -n2 |tail -n1|cut -d" " -f3
192.168.34.101

paste 合并两个文件同行号的列到一行

paste [OPTION]... [FILE]...
-d 分隔符:指定分隔符,默认用TAB
-s : 所有行合成一行显示

示例一:

默认将f1和f2合并为两列

[root@ansibledata]#seq 1 10 > f1
[root@ansibledata]#echo {a..j} | tr " " "\n" > f2
[root@ansibledata]#paste f1 f2
1   a
2   b
3   c
4   d
5   e
6   f
7   g
8   h
9   i
10  j

 示例二:

 -s 用法:横向合并

[root@ansibledata]#paste -s f1 f2
1   2   3   4   5   6   7   8   9   10
1   2   3   4   5   6   7   8   9   10

 -d用法:可以以冒号作为分隔符

[root@ansibledata]#paste -d: f1 f2
1:a
2:b
3:c
4:d
5:e
6:f
7:g
8:h
9:i
10:j
posted @ 2021-03-06 14:33  上善若水~小辉  阅读(148)  评论(0编辑  收藏  举报