鱼C论坛

 找回密码
 立即注册
查看: 3001|回复: 1

[好文转载] awk的八种内嵌变量,如NR

[复制链接]
发表于 2016-8-19 11:42:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 cory 于 2016-8-20 19:16 编辑

转载:awk的八种内嵌变量 http://cfaddnet.blog.163.com/blog/static/2184752452016719102257271/

在awk中有两类变量:
1、变量的只可以被更改,如字段分隔符(field separator)和记录分隔符(records fields)
2、变量可以用来计算和报告,如记录的个数或者字段的个数

注:需要理解Records 和Fileds,如文本:

  1. nes 2143 78 84
  2. Gondrol 2321  58 45
  3. RinRao 2122 38 37
  4. Edwin 2537  67 45
  5. Dayan 2415 30 47
复制代码


其中,默认为,总的Records个数为5,即五行内容,
每个行称为record,这个record中的fields个数为4


一、AWK FS:设置Input field separator variable
设置FS有两种方法:
1、使用-F的命令选项
2、正常变量设置值

syntax:

  1. $ awk -F 'FS' commands inputfile


  2. (or)


  3. $ awk 'BEGIN{FS="FS"}'
复制代码


注:1、FS设置的值可以是单个字符,也可以是正则表达式
2、最好在读取行内容之前,设置改变FS的值,这样就会影响读取的行。

e.g.:

  1. BEGIN{
  2. FS=":";
  3. print "Name\tUserID\tGroupID\tHomeDirectory";
  4. }
  5. {
  6. print $1"\t"$3"\t"$4"\t"$6;
  7. }
  8. END{
  9. print NR,"Records Processed"
  10. }
复制代码

读取/etc/passwd内容,命令


  1. awk -f etc_passwd.awk /etc/passwd


  2. Name        UserID        GroupID        HomeDirectory
  3. root        0        0        /root
  4. daemon        1        1        /usr/sbin
  5. bin        2        2        /bin
  6. sys        3        3        /dev
  7. sync        4        65534        /bin
  8. games        5        60        /usr/games
  9. 6 Records Processed

复制代码

二、AWK OFS : Output Field Separator Variable
与FS等价的输出形式,默认OFS是单字符空格,如:

  1. $: awk -F ':' '{print $3,$4}' /etc/passwd | head
  2. 0 0
  3. 1 1
  4. 2 2
  5. 3 3
  6. 4 65534
  7. 5 60
  8. 6 12
  9. 7 7
  10. 8 8
  11. 9 9
复制代码

默认输出的连结符为空格,即OFS为空格,更改为“=”,如:


  1. $ awk -F ':' 'BEGIN{OFS="=";}{print $3,$4;}' /etc/passwd | head
  2. 0=0
  3. 1=1
  4. 2=2
  5. 3=3
  6. 4=65534
  7. 5=60
  8. 6=12
  9. 7=7
  10. 8=8
  11. 9=9
复制代码

三、AWK RS :Records  Separator variable

记录分隔符RS
每个记录被两个新行分隔,每个字段被一个新行分隔
each records are separated by double new line, and each fields are separated by a new line character


#stored contents

  1. Jones
  2. 2143
  3. 78
  4. 84
  5. 77

  6. Gondrol
  7. 2321
  8. 56
  9. 58
  10. 45

  11. RinRao
  12. 2122
  13. 38
  14. 37
  15. 65
复制代码

利用awk脚本,输出学生姓名和学生编号,

  1. $ awk -f test.awk  students.txt
  2. Jones 2143
  3. Gondrol 2321
  4. RinRao 2122
  5. [code]
  6. 四、AWK ORS : Output Records Separator variable
  7. 与RS等价的输出形式,每个记录用这个定界符输出
  8. [code]
  9. rlk-buildsrv17@rlk-buildsrv17:~/zhanghaobin/test$ awk 'BEGIN{ORS="=";} {print;}' students.txt
  10. Jones=2143=78=84=77==Gondrol=2321=56=58=45==RinRao=2122=38=37=65=

复制代码


五、AWK NR : Numbers of  Records variable

被处理的记录的总个数或者行号,在执行语句中NR是行号,而在END语句中,代表记录的总个数

#stored contents

  1. $ cat students.txt
  2. Jones
  3. 2143
  4. 78
  5. 84
  6. 77
复制代码

输出每行行号,最终输出总记录个数:

  1. $ awk '{print "Processing  Record - ",NR;}END{print NR,"Students Records  are processed"}' students.txt
  2. Processing  Record -  1
  3. Processing  Record -  2
  4. Processing  Record -  3
  5. Processing  Record -  4
  6. Processing  Record -  5
  7. 5 Students Records  are processed

复制代码

六、AWK NF : Numbers of Fields  in a record
在一个记录里,字段的总个数
#stored contents

  1. nes 2143 78 84 77
  2. Gondrol 2321 56 58 45
  3. RinRao 2122 38 37
  4. Edwin 2537 78 67 45
  5. Dayan 2415 30 47
复制代码

每一个行内的字段总个数:

  1. $ awk '{print NR," -> ",NF}' students.txt
  2. 1  ->  5
  3. 2  ->  5
  4. 3  ->  4
  5. 4  ->  5
  6. 5  ->  4
复制代码

就会发现第三行和第五行缺少字段。

七、AWK FILENAME: Name of the current input file

FILENAME变量是正在处理的文本名称,可以读取多个输入文件,如:

  1. $ awk '{print FILENAME,"第",NR,"行"}END{print "Name of the input file: ",FILENAME}' students.txt
  2. students.txt 第 1 行
  3. students.txt 第 2 行
  4. students.txt 第 3 行
  5. students.txt 第 4 行
  6. students.txt 第 5 行
  7. Name of the input file:  students.txt
复制代码

八、Awk FNR Example: Number of Records relative to the current input file
与输入文件相关的记录总个数,如:

  1. $ awk '{print FILENAME," -> ",FNR}' students.txt  test.awk
  2. students.txt  ->  1
  3. students.txt  ->  2
  4. students.txt  ->  3
  5. students.txt  ->  4
  6. students.txt  ->  5
  7. test.awk  ->  1
  8. test.awk  ->  2
  9. test.awk  ->  3
  10. test.awk  ->  4
  11. test.awk  ->  5
  12. test.awk  ->  6
  13. test.awk  ->  7
  14. test.awk  ->  8
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-10-27 18:45:45 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 20:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表