Linux awk built-in variable introduction

  

awk is an excellent text processing tool, can be said to be a programming language. Below is the awk built-in variable.

1. Built-in variable table

Attribute description $0 Current record (as a single variable) $1~$n The nth field of the current record, separated by FS between fields FS Input field separator default Is the space NF the number of fields in the current record, that is, how many columns NR has read the number of records, that is, the line number, from the beginning of the RS input record, the separator is defaulted to the newline OFS output field separator is also a space ORS Output record separator, default is newline ARGC command line parameter number ARGV command line parameter array FILENAME current input file name IGNORECASE If true, then match case-insensitive ARGIND ARGV flag of the currently processed file CONVFMT Number conversion format %.6g ENVIRON UNIX environment variable ERRNO UNIX system error message FIELDWIDTHS Input field width blank separator string FNR Current record number OFMT number output format %.6g RSTART matched string matching RLENGTH matched string length SUBSEP \\034


2, instance

1, commonly used operations

[chengmo@localhost ~]$ awk '/^root/{print $0}' /etc/passwd root:x:0:0:root:/root:/bin/bash< Br>

/^root/is the selection expression, $0 The table is progressive



2. Setting the field separator (FS usage method)

[chengmo@localhost ~]$ awk 'BEGIN{ FS=":"}/^root/{print $1,$NF}' /etc/passwdroot /bin/bash


FS is a field separator that can be set by yourself. The default is a space, because passwd is separated by ”:”, so you need to modify the default separator. NF is the total number of fields, $0 represents the current row record, $1-$n is the current row, and each field corresponds to a value.

3, the number of records (NR, FNR use method)

[chengmo@localhost ~]$ awk 'BEGIN{FS=":"}{print NR,$1,$ NF}' /etc/passwd1 root /bin/bash2 bin /sbin/nologin3 daemon /sbin/nologin4 adm /sbin/nologin5 lp /sbin/nologin6 sync /bin/sync7 shutdown /sbin/shutdown……

NR gets the current record line

4, set the output field separator (OFS usage method)

[chengmo@localhost ~]$ awk 'BEGIN{FS=":"; OFS="^^"}/^root/{print FNR,$1,$NF}' /etc/passwd1^^root^^/bin/bash


OFS Settings Default field separator

5, set output line record separator (ORS usage method)


[chengmo@localhost ~]$ awk 'BEGIN{FS=" :";ORS="^^"}{print FNR,$1,$NF}' /etc/passwd 1 root /bin/bash^^2 bin /sbin/nologin^^3 daemon /sbin/nologin^ ^4 adm /sbin/nologin^^5 lp /sbin/nologin


From the above, ORS defaults to a newline, here changed to: ”^^”, all lines Separated by "^^”.

6. Input parameter acquisition (ARGC, ARGV use)

[chengmo@localhost ~]$ awk 'BEGIN{FS=":";print "ARGC="ARGC ;for(k in ARGV) {print k"="ARGV[k]; }}' /etc/passwdARGC=20=awk1=/etc/passwd


ARGC gets all Enter the number of parameters, ARGV gets the input parameter content, is an array.


7, get the file name passed in (use FILENAME)

[chengmo@localhost ~]$ awk 'BEGIN{FS=":";print FILENAME}{print FILENAME}' /etc/passwd

/etc/passwd

FILENAME, $0-$N,NF Cannot be used in BEGIN, BEGIN can't get any file operation Variables.


8. Get the linux environment variable (used by ENVIRON)

[chengmo@localhost ~]$ awk 'BEGIN{print ENVIRON["PATH"];}' /etc/passwd /usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/lib/ccache:/usr/lib/icecc/bin:/usr/

local/bin :/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/java/

jdk1.5.0_17/bin:/usr/java/jdk1.5.0 _17/jre/bin:/usr/local/mysql/bin:/home/web97/bin

ENVIRON is a sub-typical array that can get its value by the corresponding key value.


9. Output data format setting: (OFMT use)

[chengmo@localhost ~]$ awk 'BEGIN{OFMT="%.3f";print 2/3,123.11111111;}' /etc/passwd 0.667 123.111


The default output format of OFMT is: %.6g Keep six decimal places. Modifying OFMT here will modify the default data output format.


10, specify the separator by width (for use by FIELDWIDTHS)

[chengmo@localhost ~]$ echo 20100117054932 |

awk 'BEGIN{FIELDWIDTHS="4 2 2 2 2 3"}{print $1"-"$2"-"$3,$4":"$5":" $6}'2010-01-17 05:49:32


FIELDWIDTHS is a space-separated list of numbers used to separate records from fields, FIELDWIDTHS="4 2 2 2 2 2" means that the $1 width is 4, $2 is 2, and $3 is 2 .... This time will ignore: FS separator.


11, RSTART RLENGTH use

[chengmo@localhost ~]$ awk 'BEGIN{start=match

("this is a test" ;,/[az]+$/); print start, RSTART, RLENGTH }'11 11 4[chengmo@localhost ~]$ awk 'BEGIN

{start=match("this is a test" , /^[az]+$/); print start, RSTART, RLENGTH }'0 0 –1


RSTART is matched to the first position of the regular expression, RLENGTH matches the length of the character, Not found as -1.


The above is: awk some built-in variables, I hope there is any problem to communicate with me.

Copyright © Windows knowledge All Rights Reserved