|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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,如文本:
- nes 2143 78 84
- Gondrol 2321 58 45
- RinRao 2122 38 37
- Edwin 2537 67 45
- Dayan 2415 30 47
复制代码
其中,默认为,总的Records个数为5,即五行内容,
每个行称为record,这个record中的fields个数为4
一、AWK FS:设置Input field separator variable
设置FS有两种方法:
1、使用-F的命令选项
2、正常变量设置值
syntax:
- $ awk -F 'FS' commands inputfile
- (or)
- $ awk 'BEGIN{FS="FS"}'
复制代码
注:1、FS设置的值可以是单个字符,也可以是正则表达式
2、最好在读取行内容之前,设置改变FS的值,这样就会影响读取的行。
e.g.:
- BEGIN{
- FS=":";
- print "Name\tUserID\tGroupID\tHomeDirectory";
- }
- {
- print $1"\t"$3"\t"$4"\t"$6;
- }
- END{
- print NR,"Records Processed"
- }
复制代码
读取/etc/passwd内容,命令
- awk -f etc_passwd.awk /etc/passwd
- Name UserID GroupID HomeDirectory
- root 0 0 /root
- daemon 1 1 /usr/sbin
- bin 2 2 /bin
- sys 3 3 /dev
- sync 4 65534 /bin
- games 5 60 /usr/games
- 6 Records Processed
复制代码
二、AWK OFS : Output Field Separator Variable
与FS等价的输出形式,默认OFS是单字符空格,如:
- $: awk -F ':' '{print $3,$4}' /etc/passwd | head
- 0 0
- 1 1
- 2 2
- 3 3
- 4 65534
- 5 60
- 6 12
- 7 7
- 8 8
- 9 9
复制代码
默认输出的连结符为空格,即OFS为空格,更改为“=”,如:
- $ awk -F ':' 'BEGIN{OFS="=";}{print $3,$4;}' /etc/passwd | head
- 0=0
- 1=1
- 2=2
- 3=3
- 4=65534
- 5=60
- 6=12
- 7=7
- 8=8
- 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
- Jones
- 2143
- 78
- 84
- 77
- Gondrol
- 2321
- 56
- 58
- 45
- RinRao
- 2122
- 38
- 37
- 65
复制代码
利用awk脚本,输出学生姓名和学生编号,
- $ awk -f test.awk students.txt
- Jones 2143
- Gondrol 2321
- RinRao 2122
- [code]
- 四、AWK ORS : Output Records Separator variable
- 与RS等价的输出形式,每个记录用这个定界符输出
- [code]
- rlk-buildsrv17@rlk-buildsrv17:~/zhanghaobin/test$ awk 'BEGIN{ORS="=";} {print;}' students.txt
- 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
- $ cat students.txt
- Jones
- 2143
- 78
- 84
- 77
复制代码
输出每行行号,最终输出总记录个数:
- $ awk '{print "Processing Record - ",NR;}END{print NR,"Students Records are processed"}' students.txt
- Processing Record - 1
- Processing Record - 2
- Processing Record - 3
- Processing Record - 4
- Processing Record - 5
- 5 Students Records are processed
复制代码
六、AWK NF : Numbers of Fields in a record
在一个记录里,字段的总个数
#stored contents
- nes 2143 78 84 77
- Gondrol 2321 56 58 45
- RinRao 2122 38 37
- Edwin 2537 78 67 45
- Dayan 2415 30 47
复制代码
每一个行内的字段总个数:
- $ awk '{print NR," -> ",NF}' students.txt
- 1 -> 5
- 2 -> 5
- 3 -> 4
- 4 -> 5
- 5 -> 4
复制代码
就会发现第三行和第五行缺少字段。
七、AWK FILENAME: Name of the current input file
FILENAME变量是正在处理的文本名称,可以读取多个输入文件,如:
- $ awk '{print FILENAME,"第",NR,"行"}END{print "Name of the input file: ",FILENAME}' students.txt
- students.txt 第 1 行
- students.txt 第 2 行
- students.txt 第 3 行
- students.txt 第 4 行
- students.txt 第 5 行
- Name of the input file: students.txt
复制代码
八、Awk FNR Example: Number of Records relative to the current input file
与输入文件相关的记录总个数,如:
- $ awk '{print FILENAME," -> ",FNR}' students.txt test.awk
- students.txt -> 1
- students.txt -> 2
- students.txt -> 3
- students.txt -> 4
- students.txt -> 5
- test.awk -> 1
- test.awk -> 2
- test.awk -> 3
- test.awk -> 4
- test.awk -> 5
- test.awk -> 6
- test.awk -> 7
- test.awk -> 8
复制代码 |
|