Awk variable definition and use

Awk custom variables

1. Awk defines variables

name="hello world"

use:

[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]#                  

Two. Awk output variables

print name

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

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

use:

[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)
}

Three. Awk BEGIN+Action instance

BEGIN{
	x=10

	if(x<100){
		x=100
	}

	print x
}

Awk built-in variables

1. Review Awk language structure

BEGIN{
	actions
}

#Read external files or data

parttern{
	actions
}

END{
	actions
}

II. Common Awk built-in variables
1.FILENAME

{
	print $0
}

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

use:

[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 #Statistical
parameter number

BEGIN{
	print ARGC
}

use:

[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 #Parameter
content

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

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

use:

[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 #Input
column separator

BEGIN{
	FS="|"
}

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

use:

[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 #line
separator

BEGIN{
	RS="|"
}

{
	print $0
}

use:

[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 #The entire row of data
$N #The Nth column separated by a separator

BEGIN{
}

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

use:

[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 #Print
line number

BEGIN{
}

{
	print NR
}

END{
	print NR
}

use:

[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 #File
line number, new file will start from line 1

BEGIN{
}

{
        print NR,
}

END{
        print "----"
}

use:

[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 #Number of
columns

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

use:

[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 converts comma space characters into other characters #output
column separator

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

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

use:

[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 #Output
line separator

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

{
	print $0
}

use

[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
# By default, 6 significant digits are reserved after the decimal, but OFMT may be set to keep the number of significant digits

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

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

13.RSTART #Generally
related to match matching, the first position of the match

BEGIN{
	name="linux isisis very is much"

	match(name,/(is)+/)

	print RSTART
}

14.RLENGTH #Generally
related to match, the length of the match

BEGIN{
	name="linux isisis very is much"

	match(name,/(is)+/)

	print RSTART
	print RLENGTH
}

Guess you like

Origin blog.csdn.net/weixin_39218464/article/details/111825431