ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [Shell] awk学习(4)-read input file

[Shell] awk学习(4)-read input file

原创 Linux操作系统 作者:tolilong 时间:2016-04-21 17:18:05 0 删除 编辑
NR,FNR,NF的定义在  http://blog.itpub.net/24237320/viewspace-2072968/ 
FS,OFS,RS,ORS的定义在  http://blog.itpub.net/24237320/viewspace-2062456/


下面为其他的情况或者例子
1.更改field后,print内容会有发生变化
[/tmp/test]# awk '{print $2;$2=$2-10;print $0}' ff2        #$2发生变更后,$0内容也跟着发生变化
13
Jan 3 25 15 115


2.print虚拟列
[/tmp/test]# awk '{$6=$2+$3+$4+$5;print $6}' ff2
[/tmp/test]# awk '{$6=$2+$3+$4+$5;print $6,$0}' ff2         #会加上后面添加的$6列,增加的field会在$0中显示
168 Jan 13 25 15 115 168
.....


增加了虚拟列后,相应的NF数值也会增加
[/tmp/test]# awk '{$6=$2+$3+$4+$5;print $6,$0,NF}' ff2
168 Jan 13 25 15 115 168 6


也可以减小NF
[/tmp/test]# awk '{NF=2;print NF,$0}' ff1    #NF虽然减少了,但是$0不会发生变化


3.NF定义多个分隔符
[/tmp/test]# awk 'BEGIN{FS=" |-|/"}{print $1,$2,$3,$4,$5,$6,$7,$8}' ff1
[/tmp/test]# awk -F " |-|/" '{print $1,$2,$3,$4,$5,$6,$7,$8}' ff1


4.RS 使用空白行作为换行符
[/tmp/test]# cat ff3
Jane Doe
123 Main Street
Anywhere, SE 12345-6789


John Smith
456 Tree-lined Avenue
Smallville, MW 98765-4321


Bruce Li
789 SuZhou
JiangSu China


[/tmp/test]# awk 'BEGIN{RS="";FS="\n";OFS="  $$$  "}{print $1,$2,$3}' ff3                  #RS=""使用空白行作为换行符,
Jane Doe  $$$  123 Main Street  $$$  Anywhere, SE 12345-6789
John Smith  $$$  456 Tree-lined Avenue  $$$  Smallville, MW 98765-4321
Bruce Li  $$$  789 SuZhou  $$$  JiangSu China

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24237320/viewspace-2085546/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-07-13

  • 博文量
    406
  • 访问量
    1208676