8 Powerful Awk Built-in Variables
Category Programming Techniques
Awk has several very powerful built-in variables. Generally, they can be divided into two types: the first type is defined variables that can be changed, such as Field Separator (FS) and Record Separator (RS); the second type is used for data processing or data summarization, such as Number of Records (NR) and Number of Fields (NF). The text introduces: FS, OFS, RS, ORS, NR, NR, FNR
FS: Input Field Separator Variable
FS
(Field Separator) reads and parses each line of the input file by default according to the space as a field variable, $1, $2, etc. The FS
variable is used to set the field separator for each record. FS
can be any string or regular expression. You can declare FS in the following two ways:
Using the -F
command option
As a setting used as a common variable
Syntax:
$ awk -F 'FS' 'commands' inputfilename
or
$ awk 'BEGIN{FS="FS";}'
FS can be any character or regular expression
FS can be changed multiple times, but it will remain unchanged until it is explicitly modified. However, if you want to change the field separator, it is best to change FS
before reading the text, so that the change will take effect in the text you read.
Here is an example of using FS
to read /etc/passwd with :
as the separator
$ cat etc_passwd.awk
BEGIN{
FS=":";
print "Name\tUserID\tGroupID\tHomeDirectory";
}
{
print $1"\t"$3"\t"$4"\t"$6;
}
END {
print NR,"Records Processed";
}
Use result:
$ awk -f etc_passwd.awk /etc/passwd
Name UserID GroupID HomeDirectory
gnats 41 41 /var/lib/gnats
libuuid 100 101 /var/lib/libuuid
syslog 101 102 /home/syslog
hplip 103 7 /var/run/hplip
avahi 105 111 /var/run/avahi-daemon
saned 110 116 /home/saned
pulse 111 117 /var/run/pulse
gdm 112 119 /var/lib/gdm
8 Records Processed
OFS: Output Field Separator Variable
OFS
(Output Field Separator) is equivalent to FS
on the output side, with the default being a space character as the output separator. Here is an example of OFS
:
$ awk -F':' '{print $3,$4;}' /etc/passwd
41 41
100 101
101 102
103 7
105 111
110 116
111 117
112 119
Note the comma in the print statement of the command, which means using a space to connect two arguments, that is, the default value of OFS. Therefore, OFS
can be inserted between the output fields as follows:
$ awk -F':' 'BEGIN{OFS="=";} {print $3,$4;}' /etc/passwd
41=41
100=101
101=102
103=7
105=111
110=116
111=117
112=11
RS: Record Separator
RS
(Record Separator) defines a line of record. When reading a file, a line is taken as a record by default. The following example uses student.txt as the input file, with records separated by two blank lines, and each field of each record separated by a newline character:
$ cat student.txt
Jones
2143
78
84
77
Gondrol
2321
56
58
45
RinRao
2122
38
37
65
Edwin
2537
78
67
45
Dayan
2415
30
47
20
Then the following script will output two items from student.txt:
$ cat student.awk
BEGIN {
RS="\n\n";
FS="\n";
}
{
print $1,$2;
}
$ awk -f student.awk student.txt
Jones 2143
Gondrol 2321
RinRao 2122
Edwin 2537
Dayan 2415
In student.awk, each student's detailed