Introduction to the use of built-in variables in linux awk
Awk is an excellent text processing tool and can be said to be a programming language. The following are the built-in variables of awk that Brothers Linux Training introduces
to you.
1. Built-in variable table
Attribute description
$0 The current record (as a single variable)
$1~$n The nth field of the current record, the fields are separated by FS The
default input field separator is space
NF The number of fields in the current record, which is How many columns are there
NR The number of records that have been read, which is the line number, starting from 1
RS input records his separator defaults to newline
OFS Output field separator defaults to space
ORS output record separator, defaults to newline
ARGC command The number of line parameters
ARGV The command line parameter array
FILENAME The name of the current input file
IGNORECASE If it is true, the case-ignoring match
ARGIND The ARGV identifier of the currently processed file CONVFMT
Number conversion format %.6g
ENVIRON UNIX environment variable
ERRNO UNIX system error message
FIELDWIDTHS whitespace-separated string for input field width
FNR current record number
OFMT number output format %.6g
RSTART start of string matched by match function
RLENGTH The length of the string matched by the matching function
SUBSEP \034
2. Example
1. Common operations
[chengmo@localhost ~]$ awk'/^root/{print $0}' /etc/passwd
root:x:0:0:root :/root:/bin/bash
/^root/ is the selection expression, $0 means line by line
2, set the field separator (FS usage)
[chengmo@localhost ~]$ awk'BEGIN{FS=":"} /^root/{print $1,$NF}' /etc/passwd
root /bin/bash
FS is the field separator, you can set it yourself, the default is 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 the corresponding value of each field.
3. Number of records (NR, FNR usage)
[chengmo@localhost ~]$ awk 'BEGIN{FS=":"}{printNR,$1,$NF}' /etc/passwd
1 root /bin/bash
2 bin /sbin/nologin
3 daemon /sbin/nologin
4 adm /sbin/nologin
5 lp /sbin/nologin
6 sync /bin/sync
7 shutdown /sbin/shutdown
……
NR gets the line where the current record is located
4. Set the output field separator (OFS usage)
[chengmo@localhost ~]$ awk'BEGIN{FS=":";OFS ="^^"}/^root/{print FNR,$1,$NF}'/etc/passwd
1^^root^^/bin/bash
OFS set the default field separator
5, set the output line record separator (ORS How to use)
[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 is a newline by default, here is modified to: "^^", all lines are separated by "^^".
6. Obtaining input parameters (used by ARGC and ARGV)
[chengmo@localhost ~]$ awk'BEGIN{FS=":";print "ARGC="ARGC;for(k in ARGV) {printk"="ARGV[k] ; }}'
1=/etc/passwd
ARGC gets the number of all input parameters, ARGV gets the input parameter content, which is an array.
7. Get the incoming file name (used by FILENAME)
[chengmo@localhost ~]$ awk'BEGIN{FS=":";print FILENAME}{print FILENAME}' /etc/passwd
/etc/passwd
FILENAME,$0-$ N,NF cannot be used in BEGIN, and BEGIN cannot obtain any variables that operate with file records.
8. Obtain linux environment variables (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-canonical array, which can be accessed by the corresponding key value gets its value.
9. Output data format setting: (used by OFMT)
[chengmo@localhost ~]$ awk'BEGIN{OFMT="%.3f";print 2/3,123.11111111;}' /etc/passwd
0.667 123.111
OFMT default output format is: %.6g Six decimal places are reserved, modifying OFMT here will Modify the default data output format.
10. Specify the separator by width (used by FIELDWIDTHS)
[chengmo@localhost ~]$ echo20100117054932 | awk 'BEGIN{FIELDWIDTHS="4 2 2 2 2 3"}{print$1"-"$2"-"$3,$4": "$5":"$6}'
FIELDWIDTHS The format is a string of numbers separated by spaces to separate records. FIELDWIDTHS="4 2 2 2 2 2" means that the width of $1 is 4, $2 is 2, and $3 is 2 .... . This time it will ignore: FS separator.
11. RSTARTRLENGTH 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 is matched to the first position of the regular expression, RLENGTH matches the character length, and it is not found as -1.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326488983&siteId=291194637
Recommended
Ranking