Définition et utilisation des variables AWK

Variables personnalisées Awk

1. Awk définit les variables

name="hello world"

utilisation:

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

Deux. Variables de sortie Awk

print name

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

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

utilisation:

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

Trois. Awk BEGIN + Instance d'action

BEGIN{
	x=10

	if(x<100){
		x=100
	}

	print x
}

Variables intégrées AWK

1. Revoir la structure du langage Awk

BEGIN{
	actions
}

#Lire des fichiers ou des données externes

parttern{
	actions
}

END{
	actions
}

II. Variables
intégrées de Common Awk 1.FILENAME

{
	print $0
}

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

utilisation:

[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 #
Numéro de paramètre statistique

BEGIN{
	print ARGC
}

utilisation:

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

utilisation:

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

Séparateur de
colonne 4.FS #Input

BEGIN{
	FS="|"
}

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

utilisation:

[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
séparateur

BEGIN{
	RS="|"
}

{
	print $0
}

utilisation:

[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 #La ligne entière de données
$ N #La Nième colonne séparée par un séparateur

BEGIN{
}

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

utilisation:

[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
numéro de ligne

BEGIN{
}

{
	print NR
}

END{
	print NR
}

utilisation:

[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
numéro de ligne, le nouveau fichier commencera à partir de la ligne 1

BEGIN{
}

{
        print NR,
}

END{
        print "----"
}

utilisation:

[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 #Nombre de
colonnes

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

utilisation:

[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 convertit les caractères d'espace virgule en d'autres caractères #
séparateur de colonne de sortie

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

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

utilisation:

[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 #
Séparateur de ligne de sortie

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

{
	print $0
}

utilisation

[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
# Par défaut, 6 chiffres significatifs sont réservés après la décimale, mais OFMT peut être configuré pour conserver le nombre de chiffres significatifs

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

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

13.RSTART #Généralement
lié à la correspondance de correspondance, la première position de la correspondance

BEGIN{
	name="linux isisis very is much"

	match(name,/(is)+/)

	print RSTART
}

14.RLENGTH #Généralement
lié au match, la durée du match

BEGIN{
	name="linux isisis very is much"

	match(name,/(is)+/)

	print RSTART
	print RLENGTH
}

Je suppose que tu aimes

Origine blog.csdn.net/weixin_39218464/article/details/111825431
conseillé
Classement