Linux命令之awk:变量应用(二)

awk内置变量(预定义变量)

说明:[A][N][P][G]表示第一个支持变量的工具,[A]=awk、[N]=nawk、[P]=POSIXawk、[G]=gawk

  • $n 当前记录的第n个字段,比如n为1表示第一个字段,n为2表示第二个字段。
  • $0 这个变量包含执行过程中当前行的文本内容。
  • [A] FS :字段分隔符(默认是任何空格)。
  • [A] NF :表示字段数,在执行过程中对应于当前的字段数。
  • [A] NR :表示记录数,在执行过程中对应于当前的行号。
  • [A] FILENAME : 当前输入文件的名。
  • [A] OFMT :数字的输出格式(默认值是%.6g)。
  • [A] OFS :输出字段分隔符(默认值是一个空格)。
  • [A] ORS :输出记录分隔符(默认值是一个换行符)。
  • [A] RS :记录分隔符(默认是一个换行符)。
  • [N] ARGC :命令行参数的数目。
  • [N] ARGV :包含命令行参数的数组。
  • [N] ERRNO :最后一个系统错误的描述。
  • [N] RSTART :由match函数所匹配的字符串的第一个位置。
  • [N] RLENGTH :由match函数所匹配的字符串的长度。
  • [N] SUBSEP 数组下标分隔符(默认值是34)。
  • [P] ENVIRON :环境变量关联数组。
  • [P] FNR :同NR,但相对于当前文件。
  • [G] ARGIND :命令行中当前文件的位置(从0开始算)。
  • [G] CONVFMT :数字转换格式(默认值为%.6g)。
  • [G] FIELDWIDTHS :字段宽度列表(用空格键分隔)。
  • [G] IGNORECASE :如果为真,则进行忽略大小写的匹配。

示例

NR纪录数
NF字段数
FILENAME文件名
ARGC命令行参数数量
[root@sxooky ~]# echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" > a.txt
[root@sxooky ~]# echo -e "test1 f2 f3 a1\ntest2 f4 f5 a2\ntest3 f6 f7 a3 a4" > b.txt
[root@sxooky ~]# awk '{print "NR:"NR,"NF:"NF,"FILENAME:"FILENAME,"ARGC:"ARGC}' a.txt b.txt 
NR:1 NF:3 FILENAME:a.txt ARGC:3
NR:2 NF:3 FILENAME:a.txt ARGC:3
NR:3 NF:3 FILENAME:a.txt ARGC:3
NR:4 NF:4 FILENAME:b.txt ARGC:3
NR:5 NF:4 FILENAME:b.txt ARGC:3
NR:6 NF:5 FILENAME:b.txt ARGC:3
NR变量的应用(纪录数)

统计文件中的行数:

[root@sxooky ~]# cat a.txt
line1 f2 f3
line2 f4 f5
line3 f6 f7
[root@sxooky ~]# awk 'END{print "NR_total:"NR}' a.txt
NR_total:3

以上命令只使用了END语句块,在读入每一行的时,awk会将NR更新为对应的行号,当到达最后一行NR的值就是最后一行的行号,所以END语句块中的NR就是文件的行数。

每行中第一个字段的累加:

[root@sxooky ~]# seq 5 | awk 'BEGIN{ sum=0; print "求各字段累加总和:" } { print $1"+"; sum+=$1 } END{ print "等于"; print sum }'
求各字段累加总和:
1+
2+
3+
4+
5+
等于
15
ORS变量的应用

ORS输出记录分隔符,缺省则为换行(“\n”)  如上例

上例中将其内容连接到一行:

[root@sxooky ~]# seq 5 | awk 'BEGIN{ sum=0; print "求各字段累加总和:";ORS="" }NR==1{print $1;sum+=$1}NR>=2{print "+"$1;sum+=$1}END{ print "="; print sum "\n"}'
求各字段累加总和:
1+2+3+4+5=15
OFS变量的应用

OFS输出字段分隔符,默认为空(没有)。

[root@sxooky ~]# cat a.txt 
line1 f2 f3
line2 f4 f5
line3 f6 f7
[root@sxooky ~]# awk 'BEGIN{OFS=" - "}{print $1,$2,$3}' a.txt 
line1 - f2 - f3
line2 - f4 - f5
line3 - f6 - f7
[root@sxooky ~]# awk 'BEGIN{OFS="......"}{print $1,$2,$3}' a.txt 
line1......f2......f3
line2......f4......f5
line3......f6......f7
RS变量控制记录分隔符
[root@sxooky ~]# awk 'BEGIN{ORS=" - "}{print}END{ORS="\n";print "\n"}' a.txt
line1 f2 f3 - line2 f4 f5 - line3 f6 f7 - 

[root@sxooky ~]# awk 'BEGIN{ORS=" - "}{print}END{ORS="\n";print "\n"}' a.txt |awk 'BEGIN{RS=" - "}{print $1}'
line1
line2
line3
[root@sxooky ~]# tail -1 /etc/passwd
sxooky:x:3367:3367::/home/sxooky:/bin/bash
[root@sxooky ~]# tail -1 /etc/passwd |awk 'BEGIN{RS=":"}{print $1}'
sxooky
x
3367
3367

/home/sxooky
/bin/bash
[root@sxooky ~]# tail -1 /etc/passwd |awk 'BEGIN{RS="/"}{print $1}'
sxooky:x:3367:3367::
home
sxooky:
bin
bash
NF变量的应用(打印某个字段)
[root@sxooky ~]# cd /usr/local/apache2.4/htdocs/wordpress/
[root@sxooky wordpress]# pwd
/usr/local/apache2.4/htdocs/wordpress
[root@sxooky wordpress]# pwd |awk -F"/" '{print $0}'
/usr/local/apache2.4/htdocs/wordpress
#打印最后一个字段或倒数第二个字段
[root@sxooky wordpress]# pwd |awk -F"/" '{print $NF}'
wordpress
[root@sxooky wordpress]# pwd |awk -F"/" '{print $(NF-1)}'
htdocs

awk外部变量

一般变量

在awk中,设置有意义的域名是一种好习惯一般的变量名设置方式为 var = $n,这里var    为调用的域变量名, n为实际域号。

  • 找出uid>1000的用户
[root@sxooky ~]# tail -1 /etc/passwd
sxooky:x:3367:3367::/home/sxooky:/bin/bash
[root@sxooky ~]# awk -F: '{name=$1;id=$3;if(id>1000)print name "\t" id}' /etc/passwd
mysql	3366
sxooky	3367

统计当前文件下面所有文件的大小之和:

  • 统计除目录外的文件大小总和
[root@sxooky ~]# ls -al |awk 'BEGIN{total=0}{if(/^[^d]/){total+=$5;print $5"\t"$9}}END{print "total size:" total}'
	
36	a.txt
18	awk_var.txt
14522	.bash_history
18	.bash_logout
176	.bash_profile
176	.bashrc
48	b.txt
100	.cshrc
74454	logssh.txt
893	.mysql_history
64	.pydistutils.cfg
129	.tcshrc
8979	.viminfo
total size:99613
将外部变量值传递给awk
  • 借助-v选项,可以将外部值(并非来自stdin)传递给awk:
  • 另一种传递外部变量方法
  • 当输入来自于文件时使用
[root@sxooky ~]# VAR=12345
[root@sxooky ~]# echo |awk -v var=$VAR '{print var}'
12345
[root@sxooky ~]# var1="abc"
[root@sxooky ~]# var2="123"
[root@sxooky ~]# echo |awk '{print v1 "\t" v2}' v1=$var1 v2=$var2
abc	123
[root@sxooky ~]# vim awk_var.txt

以上方法中,变量之间用空格分隔作为awk的命令行参数跟随在BEGIN、{}和END语句块之后。

转载于:https://my.oschina.net/u/3409834/blog/1548743

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
For anyone who writes scripts in the awk family of languages, the third edition of Effective awk Programming provides an in-depth guide to processing text files with plenty of working sample code. Whether you are starting out with awk or are an experienced developer, this book will help you extend the reach of your awk scripts., This tutorial covers the entire spectrum of awk script development: From the basics of opening, searching, and transforming text files, to a comprehensive tutorial for regular expressions, to more advanced features like internetworking. The focus is on the practical side of creating and running awk scripts, and there's plenty of hands-on advice for installing and running today's awk (and gawk)., The book begins with the fundamentals of awk for opening and transforming text flat files. The coverage of regular expressions, from simple rules for matching text to more advanced options, is particularly solid. You learn how to add variables and expressions for more intelligent awk scripts, plus how to parse data into records and fields. You'll also find out how to redirect output from awk scripts to other programs, a useful technique that can cause awk to get a lot more done in real applications., Later, you learn several valuable sample awk scripts that mimic existing Unix utilities (like grep, id, and split), plus samples for counting words in documents and printing mailing labels, and even a stream editor. This grab bag of sample code lets you try out the techniques presented earlier in the book. Other sections look at support for networking in today's gawk; for example, how gawk can read and write to URLs on the network almost just as easily as local files. Full sample code will teach the beginner or expert how to get productive with networks and awk. Final appendices trace the evolution of the awk language and show you how to download and install gawk., Suitable for beginner and experienced awk developers, Effective awk Programming, Third Edi

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值