Awk变量定义与使用

Awk自定义变量

一.Awk定义变量

name="hello world"

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk
hello world
[root@VM-0-6-centos mnt]# cat test.awk
BEGIN{
        name="hello world"
        print name
}
[root@VM-0-6-centos mnt]#                  

二.Awk输出变量

print name

print "#",name,"#"
print "#" name "#"

printf("#%s#\n",name);
printf("# %s #\n",name)

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk
hello world
# hello world #
#hello world#
#hello world#
# hello world #
[root@VM-0-6-centos mnt]# cat test.awk
BEGIN{
        name="hello world"

        print name

        print "#",name,"#"
        print "#" name "#"

        printf("#%s#\n",name);
        printf("# %s #\n",name)
}

三.Awk BEGIN+Action实例

BEGIN{
	x=10

	if(x<100){
		x=100
	}

	print x
}

Awk内置变量

一.复习Awk语言结构

BEGIN{
	actions
}

#读取外部文件或数据

parttern{
	actions
}

END{
	actions
}

二.常用Awk内置变量
1.FILENAME

{
	print $0
}

END{
	print "--------------------"
	print FILENAME,"read complete!"
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat
A125 Jenny 100 210
A341 Dan 110 215
P158 Max 130 209
P148 John 125 220
A123 Linda 95 210
--------------------
emp.dat read complete!
[root@VM-0-6-centos mnt]# cat test.awk
{
        print $0
}

END{
        print "--------------------"
        print FILENAME,"read complete!"
}
[root@VM-0-6-centos mnt]#

2.ARGC
#统计参数个数

BEGIN{
	print ARGC
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat
2
[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat test.awk
3
[root@VM-0-6-centos mnt]#

3.ARGV
#参数内容

BEGIN{
	# for(i=0;i<ARGC;i++){
	# 	print i,ARGV[i]
	# }

	i=0
	while(i<ARGC){
		print i,ARGV[i]
		i++
	}
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat
0 awk  #0代表程序本身,可以忽略不计
1 emp.dat
[root@VM-0-6-centos mnt]#

4.FS
#输入列分割符

BEGIN{
	FS="|"
}

{
	print $1,"->",$2,"->",$3,"->",$4
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk rs.dat
linux_10 -> php_20 -> java_30 -> python_40
[root@VM-0-6-centos mnt]# cat rs.dat
linux_10|php_20|java_30|python_40
[root@VM-0-6-centos mnt]#

5.RS
#行分割符

BEGIN{
	RS="|"
}

{
	print $0
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk rs.dat
linux_10
php_20
java_30
python_40

[root@VM-0-6-centos mnt]# cat rs.dat
linux_10|php_20|java_30|python_40
[root@VM-0-6-centos mnt]#

6.$N
$0 #整行数据
$N #用分割符分开的第N列

BEGIN{
}

{
	print $0,$1,$2,$3,$4
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat
A125 Jenny 100 210 A125 Jenny 100 210
A341 Dan 110 215 A341 Dan 110 215
P158 Max 130 209 P158 Max 130 209
P148 John 125 220 P148 John 125 220
A123 Linda 95 210 A123 Linda 95 210
[root@VM-0-6-centos mnt]# cat emp.dat
A125 Jenny 100 210
A341 Dan 110 215
P158 Max 130 209
P148 John 125 220
A123 Linda 95 210
[root@VM-0-6-centos mnt]#

7.NR
#打印行数

BEGIN{
}

{
	print NR
}

END{
	print NR
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat 
1                                                 
2                                                 
3                                                 
4                                                 
5                                                 
5                                                 
[root@VM-0-6-centos mnt]# cat emp.dat             
A125 Jenny 100 210                                
A341 Dan 110 215                                  
P158 Max 130 209                                  
P148 John 125 220                                 
A123 Linda 95 210                                 
[root@VM-0-6-centos mnt]#                         

8.FNR
#文件行数,新文件会从第1行开始

BEGIN{
}

{
        print NR,
}

END{
        print "----"
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat emp.dat emp.dat
1 1
2 2
3 3
4 4
5 5
6 1
7 2
8 3
9 4
10 5
11 1
12 2
13 3
14 4
15 5
----
[root@VM-0-6-centos mnt]#

9.NF
#列数

BEGIN{               
}                    
                     
{                    
        print NR,NF  
}                    
                     
END{                 
        print "----" 
}                    

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk emp.dat emp.dat emp.dat
1 4
2 4
3 4
4 4
5 4
6 4
7 4
8 4
9 4
10 4
11 4
12 4
13 4
14 4
15 4
----                     

10.OFS 将逗号空格符转换成其他字符
#输出列分割符

BEGIN{
	FS="|"
	OFS="->"
}

{
	print $1,$2,$3,$4
}

使用:

[root@VM-0-6-centos mnt]# awk -f test.awk rs.dat
linux_10->php_20->java_30->python_40
[root@VM-0-6-centos mnt]# cat rs.dat
linux_10|php_20|java_30|python_40
[root@VM-0-6-centos mnt]#

11.ORS
#输出行分割符

BEGIN{
	RS="|"
	ORS="->"
}

{
	print $0
}

使用

[root@VM-0-6-centos mnt]# awk -f test.awk rs.dat
linux_10->php_20->java_30->python_40
->[root@VM-0-6-centos mnt]# cat rs.dat
linux_10|php_20|java_30|python_40
[root@VM-0-6-centos mnt]#

12.OFMT
#默认小数后面保留6位有效数字,但OFMT可能设置保留有效数字位数

BEGIN{
	OFMT="%.3g"
	print 2/3
}

#与printf中%.2g用法相同
BEGIN{
	printf("----%.2g----\n",2/3)
}

13.RSTART
#一般是与match匹配有关,匹配的首位置

BEGIN{
	name="linux isisis very is much"

	match(name,/(is)+/)

	print RSTART
}

14.RLENGTH
#一般是与match匹配有关,匹配到的长度

BEGIN{
	name="linux isisis very is much"

	match(name,/(is)+/)

	print RSTART
	print RLENGTH
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值