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

 

 

 

     

 

     

相关文章

系ubuntu 下面打开终端输入:sudo apt-get install sendmail...
依家我有1个软件goagent目录(大家懂得) 放在/home/gateman/...
其实我想讲的是 cp -L关于-L参数的解释:-L, --dereferenc...
原地址:http://www.rjgc.net/control/content/content.php?...
chroot,即 change root directory (更改 root 目录)。在 li...
简单解析下, stdin就是标准输入, stdout就是标准。举个例子...