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
复制代码

 

 

 

     

 

     

posted @   小鲨鱼2018  阅读(304)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示