linux系统awk命令内置变量
1、创建测试数据
[root@linuxprobe test]# cat > a.txt
<1 01 02 03 04
<2 05 06 07 08
<3 09 10
<4 13 14 15 16
<5 17 18 19 20
<6 21
^C
[root@linuxprobe test]# cat a.txt ## 测试数据
<1 01 02 03 04
<2 05 06 07 08
<3 09 10
<4 13 14 15 16
<5 17 18 19 20
<6 21
[root@linuxprobe test]#
2、NF
NF
[root@linuxprobe test]# cat a.txt <1 01 02 03 04 <2 05 06 07 08 <3 09 10 <4 13 14 15 16 <5 17 18 19 20 <6 21 [root@linuxprobe test]# awk '{print NF}' a.txt ## NF记录读取数据的每行分割的字段数,用于统计每行有多少列 5 5 3 5 5 2 [root@linuxprobe test]# awk '{print $NF}' a.txt ## 提取每一行的最后一个字段 04 08 10 16 20 21
3、NR
NR
[root@linuxprobe test]# cat a.txt
<1 01 02 03 04
<2 05 06 07 08
<3 09 10
<4 13 14 15 16
<5 17 18 19 20
<6 21
[root@linuxprobe test]# awk '{print NR}' a.txt ## NR记录数据读取的行数
1
2
3
4
5
6
[root@linuxprobe test]# awk 'END{print NR}' a.txt ## 最后输出行数,用于统计行数
6
[root@linuxprobe test]# awk 'NR == 3' a.txt ## 提取第三行
<3 09 10
[root@linuxprobe test]# awk 'NR > 3' a.txt ## 同上
<4 13 14 15 16
<5 17 18 19 20
<6 21
[root@linuxprobe test]# awk 'NR > 2 && NR < 5' a.txt ## && 在awk中是逻辑操作符,表示而且
<3 09 10
<4 13 14 15 16
[root@linuxprobe test]# awk 'NR < 3 || NR > 5' a.txt ## || 在awk中 表示或者
<1 01 02 03 04
<2 05 06 07 08
<6 21
4、FS
[root@linuxprobe test]# seq -w 24 | xargs -n 4 | sed = | sed 'N;s/\n/ /' | sed 's/^/>/' > 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
[root@linuxprobe test]# awk '{print $2,$4}' a.txt ## 提取第2、4列,默认的分隔符是空格
01 03
05 07
09 11
13 15
17 19
21 23
[root@linuxprobe test]# tr " " "," < a.txt > a && mv a 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
[root@linuxprobe test]# awk '{print $2,$4}' a.txt ## 默认的读入分割符是空格,因此无法提取第2、4列
[root@linuxprobe test]# awk 'BEGIN{FS = ","}{print $2,$4}' a.txt ## 使用FS指定读入分隔符为逗号,可以提取第2、4列
01 03
05 07
09 11
13 15
17 19
21 23
5、0FS
[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,$4}' a.txt ## 提取第2、4行,默认输出分割符为空格
01 03
05 07
09 11
13 15
17 19
21 23
[root@linuxprobe test]# awk 'BEGIN{OFS = "-"}{print $2,$4,$5}' a.txt ## 使用OFS制动输出分隔符为-.
01-03-04
05-07-08
09-11-12
13-15-16
17-19-20
21-23-24
6、RS
RS
[root@linuxprobe test]# cat -A a.txt
<1 01 02 03 04$
<2 05 06 07 08$
<3 09 10 11 12$
[root@linuxprobe test]# awk '{print $0}' a.txt ## awk 默认读入数据每行的分隔符是换行符
<1 01 02 03 04
<2 05 06 07 08
<3 09 10 11 12
[root@linuxprobe test]# awk '{RS = " "}{print $0}' a.txt ##RS表示读入数据每行的分隔符
<1 01 02 03 04
<2
05
06
07
08
<3
09
10
11
12
[root@linuxprobe test]# awk 'BEGIN{RS = " "}{print $0}' a.txt ## 设置空格为每行的读入分隔符
<1
01
02
03
04
<2
05
06
07
08
<3
09
10
11
12
7、ORS
[root@linuxprobe test]# cat -A a.txt
<1 01 02 03 04$
<2 05 06 07 08$
<3 09 10 11 12$
[root@linuxprobe test]# awk '{print $0}' a.txt ## 默认的输出换行符为换行符\n,
<1 01 02 03 04
<2 05 06 07 08
<3 09 10 11 12
[root@linuxprobe test]# awk '{ORS = "!"}{print $0}' a.txt ## ORS 设置输出换行符为 !
<1 01 02 03 04!<2 05 06 07 08!<3 09 10 11 12![root@linuxprobe test]#
8、FILENAME
FILENAME
[root@linuxprobe test]# cat a.txt
<1 01 02 03 04
<2 05 06 07 08
<3 09 10
<4 13 14 15 16
<5 17 18 19 20
<6 21
[root@linuxprobe test]# awk '{print FILENAME,$0}' a.txt ## FILENAME记录文件名
a.txt <1 01 02 03 04
a.txt <2 05 06 07 08
a.txt <3 09 10
a.txt <4 13 14 15 16
a.txt <5 17 18 19 20
a.txt <6 21
[root@linuxprobe test]# awk '{print $2,FILENAME}' a.txt ## 同上
01 a.txt
05 a.txt
09 a.txt
13 a.txt
17 a.txt
21 a.txt
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律