Thisarticle is part of the on-goingAwk Tutorial Examplesseries. Awk has several powerful built-in variables. There are two types of built-in variables in Awk.
1 Variable which defines values which can be changed such as field separator and record separator.
2 Variable which can be used for processing and reports such asNumber of records, number of fields.
1. Awk FS Example: Input field separator variable.
Awk reads and parses each line from input based on white space character by defaultand set the variables $1,$2 and etc. Awk FS variable is used to set the fieldseparator for each record. Awk FS can be set to any single character or regular expression. You can use input field separator using one of the following twooptions:
3 Using -F command line option.
4 Awk FS can be set like normal variable.
Syntax:
$ awk-F 'FS' 'commands' inputfilename
(or)
$ awk 'BEGIN{FS="FS";}'
§ Awk FS is any single character or regular expression which you want to use as a input field separator.
§ Awk FS can be changed any number of times, it retains its valuesuntil it is explicitly changed. If you want to change the field separator, itsbetter to change before you read the line. So that change affects the line what you read.
Here is an awk FS example to read the /etc/passwd file which has“:” as field delimiter.
$ catetc_passwd.awk
BEGIN{
FS=":";
print"Name\tUserID\tGroupID\tHomeDirectory";
}
{
print$1"\t"$3"\t"$4"\t"$6;
}
END {
print NR,"Records Processed";
}
$awk -f etc_passwd.awk /etc/passwd
Name UserID GroupID HomeDirectory
gnats 41 41 /var/lib/gnats
libuuid100 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 112119 /var/lib/gdm
8Records Processed
2. Awk OFS Example: Output Field Separator Variable
Awk OFS is an output equivalent of awk FS variable. By default awk OFS is a single space character. Following is an awk OFS example.
$ awk-F':' '{print $3,$4;}' /etc/passwd
41 41
100 101
101 102
103 7
105 111
110 116
111 117
112 119
Concatenatorin the print statement“,”concatenates two parameters with a space which is the value of awk OFS bydefault. So, Awk OFS value will be inserted between fields in the output asshown below.
$ awk -F':' 'BEGIN{OFS="=";} {print $3,$4;}'/etc/passwd
41=41
100=101
101=102
103=7
105=111
110=116
111=117
112=119
3. Awk RS Example: Record Separator variable
Awk RS defines a line. Awk reads line by line by default.
Let ustake students marks are stored in a file, each records are separated by doublenew line, and each fields are separated by a new line character.
$catstudent.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
Now thebelow Awk script prints the Student name and Rollno from the above input file.
$catstudent.awk
BEGIN {
RS="\n\n";
FS="\n";
}
{
print $1,$2;
}
$ awk-f student.awk student.txt
Jones2143
Gondrol2321
RinRao2122
Edwin2537
Dayan2415
In thescript student.awk, it reads each student detail as a single record,because awkRS has been assigned to double new line character and each line in a record isa field, since FS is newline character.
4. Awk ORS Example: Output Record Separator Variable
Awk ORS is an Output equivalent of RS. Each record in the outputwill be printed with this delimiter. Following is an awk ORS example:
$ awk 'BEGIN{ORS="=";} {print;}'student-marks
Jones2143 78 84 77=Gondrol 2321 56 58 45=RinRao 2122 38 37 65=Edwin 2537 78 6745=Dayan 2415 30 47 20=
In theabove script,each records in the file student-marks file is delimited by thecharacter“=”.
5. Awk NR Example: Number of Records Variable
Awk NR gives you the total number of records being processed orline number. In the following awk NR example, NR variable has line number, inthe END section awk NR tells you the total number of records in a file.
$ awk'{print "Processing Record - ",NR;}END {print NR, "StudentsRecords are processed";}' student-marks
ProcessingRecord - 1
ProcessingRecord - 2
ProcessingRecord - 3
ProcessingRecord - 4
ProcessingRecord - 5
5Students Records are processed
6. Awk NF Example: Number of Fields in a record
Awk NF gives you the total number of fields in a record. Awk NF willbe very useful for validating whether all the fields are exist in a record.
Let ustake in the student-marks file, Test3 score is missing for to students as shownbelow.
$catstudent-marks
Jones2143 78 84 77
Gondrol2321 56 58 45
RinRao2122 38 37
Edwin2537 78 67 45
Dayan2415 30 47
Thefollowing Awk script, prints Record(line) number, and number of fields in thatrecord. So It will be very simple to find out that Test3 score is missing.
$ awk'{print NR,"->",NF}' student-marks
1 ->5
2 ->5
3 ->4
4 ->5
5 ->4
7. Awk FILENAME Example: Name of the current input file
FILENAMEvariable gives the name of the file being read. Awk can accept number of inputfiles to process.
$ awk'{print FILENAME}' student-marks
student-marks
student-marks
student-marks
student-marks
student-marks
In theabove example, it prints the FILENAME i.e student-marks for each record of theinput file.
8. Awk FNR Example: Number of Records relative to the currentinput file
Whenawk reads from the multiple input file, awk NR variable will give the totalnumber of records relative to all the input file.Awk FNR will give you number of records for each input file.
$ awk '{print FILENAME, FNR;}' student-marks bookdetails
student-marks1
student-marks2
student-marks3
student-marks4
student-marks5
bookdetails1
bookdetails2
bookdetails3
bookdetails4
bookdetails5
In theabove example, instead of awk FNR, if you use awk NR, for the file bookdetailsthe you will get from 6 to 10 for each record.
################################################################################################
容易混淆的两个变量:NF和$NF两个变量
NF是处理行中域的个数
$NF 是输出处理行中最后一个域的内容
$1 处理行中第一个域的内容 ,依此类推