linux系统中awk命令提取特定列、添加列
1、创建测试数据
[root@linuxprobe test]# seq -w 24 | xargs -n 4 | awk '{print "<"NR,$0}' > a.txt ## 创建测试数据
[root@linuxprobe test]# cat a.txt
<1 01 02 03 04
<2 05 06 07 08
<3 09 10 11 12
<4 13 14 15 16
<5 17 18 19 20
<6 21 22 23 24
2、提取特定列
[root@linuxprobe test]# awk '{print $2}' a.txt ## awk读取数据的默认分隔符是空格,提取第二列
01
05
09
13
17
21
[root@linuxprobe test]# awk '{print $2,$4}' a.txt ##提取第二列和第四列,不同字段间用逗号隔开,默认输出分隔符为空格
01 03
05 07
09 11
13 15
17 19
21 23
[root@linuxprobe test]# awk '{print $4,$2,$1}' a.txt ## 变换列的顺序
03 01 <1
07 05 <2
11 09 <3
15 13 <4
19 17 <5
23 21 <6
[root@linuxprobe test]# awk '{print $NF}' a.txt ## 提取最后一列,NF是awk的内置变量,记录每一行分割的字段数,最后一个字段即是最后一列
04
08
12
16
20
24
[root@linuxprobe test]# awk '{print $(NF-1)}' a.txt ## 同理,提取倒数第二列
03
07
11
15
19
23
3、指定读入数据和输出数据的分割符
[root@linuxprobe test]# sed 's/ /,/g' a.txt -i ## 改变测试数据,将空格替换为逗号
[root@linuxprobe test]# cat a.txt
<1,01,02,03,04
<2,05,06,07,08
<3,09,10,11,12
<4,13,14,15,16
<5,17,18,19,20
<6,21,22,23,24
[root@linuxprobe test]# awk '{print $2}' a.txt ## awk默认以空格为读入分割符,仅有1列
[root@linuxprobe test]# awk -F "," '{print $2}' a.txt ## 利用 -F选项 指定逗号为分割符,提取第二列
01
05
09
13
17
21
[root@linuxprobe test]# awk -F "," '{print $2,$5}' a.txt ## 同上
01 04
05 08
09 12
13 16
17 20
21 24
[root@linuxprobe test]# awk 'FS = "," {print $1}' a.txt ## ???
<1,01,02,03,04
<2
<3
<4
<5
<6
[root@linuxprobe test]# awk 'BEGIN{FS = ","}{print $1}' a.txt ## 定价与 -F
<1
<2
<3
<4
<5
<6
4、指定输出分隔符
[root@linuxprobe test]# cat a.txt
<1,01,02,03,04
<2,05,06,07,08
<3,09,10,11,12
<4,13,14,15,16
<5,17,18,19,20
<6,21,22,23,24
[root@linuxprobe test]# awk -F "," '{print $2,$4,$5}' a.txt ##默认输出分割符为空格
01 03 04
05 07 08
09 11 12
13 15 16
17 19 20
21 23 24
[root@linuxprobe test]# awk -F "," '{OFS = "\t"} {print $2,$4,$5}' a.txt ## 指定制表符为输出分隔符
01 03 04
05 07 08
09 11 12
13 15 16
17 19 20
21 23 24
[root@linuxprobe test]# awk -F "," '{OFS = ":"} {print $2,$4,$5}' a.txt ## 指定冒号为输出分隔符
01:03:04
05:07:08
09:11:12
13:15:16
17:19:20
21:23:24
5、awk添加列
[root@linuxprobe test]# sed 's/,/ /g' a.txt -i ##改变测试数据
[root@linuxprobe test]# cat a.txt
<1 01 02 03 04
<2 05 06 07 08
<3 09 10 11 12
<4 13 14 15 16
<5 17 18 19 20
<6 21 22 23 24
[root@linuxprobe test]# awk '{print "xxx",$2}' a.txt ## 添加xxx,用双引号引起来
xxx 01
xxx 05
xxx 09
xxx 13
xxx 17
xxx 21
[root@linuxprobe test]# awk '{print $3,"yyy",$5}' a.txt ##同上
02 yyy 04
06 yyy 08
10 yyy 12
14 yyy 16
18 yyy 20
22 yyy 24
[root@linuxprobe test]# awk '{print $2,"mmm","nnn",$5,"zzz"}' a.txt ## 同上
01 mmm nnn 04 zzz
05 mmm nnn 08 zzz
09 mmm nnn 12 zzz
13 mmm nnn 16 zzz
17 mmm nnn 20 zzz
21 mmm nnn 24 zzz
[root@linuxprobe test]# awk '{print "xxxx",$0}' a.txt ## $0表示当前行的所有
xxxx <1 01 02 03 04
xxxx <2 05 06 07 08
xxxx <3 09 10 11 12
xxxx <4 13 14 15 16
xxxx <5 17 18 19 20
xxxx <6 21 22 23 24
[root@linuxprobe test]# awk '{print NR,$1,$3}' a.txt ## NR是awk的内置变量,用于记录读取数据的行数
1 <1 02
2 <2 06
3 <3 10
4 <4 14
5 <5 18
6 <6 22
[root@linuxprobe test]# awk '{print $1,NR,$3}' a.txt ## 同上
<1 1 02
<2 2 06
<3 3 10
<4 4 14
<5 5 18
<6 6 22
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了