1. Awk FS Example: Input field separator variable.
Awk reads and parses each line from input based on whitespace character by default and set the variables $1,$2 and etc. Awk FS variable is used to set the field separator 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 two options:
- Using -F command line option.
- 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 values until it is explicitly changed. If you want to change the field separator, its better 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.
$ cat etc_passwd.awkBEGIN{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/passwdName UserID GroupID HomeDirectorygnats 41 41 /var/lib/gnatslibuuid 100 101 /var/lib/libuuidsyslog 101 102 /home/sysloghplip 103 7 /var/run/hplipavahi 105 111 /var/run/avahi-daemonsaned 110 116 /home/sanedpulse 111 117 /var/run/pulsegdm 112 119 /var/lib/gdm8 Records 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/passwd41 41100 101101 102103 7105 111110 116111 117112 119
Concatenator in the print statement “,” concatenates two parameters with a space which is the value of awk OFS by default. So, Awk OFS value will be inserted between fields in the output as shown below.
$ awk -F':' 'BEGIN{OFS="=";} {print $3,$4;}' /etc/passwd41=41100=101101=102103=7105=111110=116111=117112=119
3. Awk RS Example: Record Separator variable
Awk RS defines a line. Awk reads line by line by default.
Let us take students marks are stored in a file, each records are separated by double new line, and each fields are separated by a new line character.
$cat student.txtJones2143788477Gondrol2321565845RinRao2122383765Edwin2537786745Dayan2415304720
Now the below Awk script prints the Student name and Rollno from the above input file.
$cat student.awkBEGIN { RS="\n\n"; FS="\n";}{ print $1,$2;}$ awk -f student.awk student.txtJones 2143Gondrol 2321RinRao 2122Edwin 2537Dayan 2415
In the script student.awk, it reads each student detail as a single record,because awk RS has been assigned to double new line character and each line in a record is a 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 output will be printed with this delimiter. Following is an awk ORS example:
$ awk 'BEGIN{ORS="=";} {print;}' student-marksJones 2143 78 84 77=Gondrol 2321 56 58 45=RinRao 2122 38 37 65=Edwin 2537 78 67 45=Dayan 2415 30 47 20=
In the above script,each records in the file student-marks file is delimited by the character “=”.
5. Awk NR Example: Number of Records Variable
Awk NR gives you the total number of records being processed or line number. In the following awk NR example, NR variable has line number, in the END section awk NR tells you the total number of records in a file.
$ awk '{print "Processing Record - ",NR;}END {print NR, "Students Records are processed";}' student-marksProcessing Record - 1Processing Record - 2Processing Record - 3Processing Record - 4Processing Record - 55 Students 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 will be very useful for validating whether all the fields are exist in a record.
Let us take in the student-marks file, Test3 score is missing for to students as shown below.
$cat student-marksJones 2143 78 84 77Gondrol 2321 56 58 45RinRao 2122 38 37Edwin 2537 78 67 45Dayan 2415 30 47
The following Awk script, prints Record(line) number, and number of fields in that record. So It will be very simple to find out that Test3 score is missing.
$ awk '{print NR,"->",NF}' student-marks1 -> 52 -> 53 -> 44 -> 55 -> 4
7. Awk FILENAME Example: Name of the current input file
FILENAME variable gives the name of the file being read. Awk can accept number of input files to process.
$ awk '{print FILENAME}' student-marksstudent-marksstudent-marksstudent-marksstudent-marksstudent-marks
In the above example, it prints the FILENAME i.e student-marks for each record of the input file.
8. Awk FNR Example: Number of Records relative to the current input file
When awk reads from the multiple input file, awk NR variable will give the total number 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 bookdetailsstudent-marks 1student-marks 2student-marks 3student-marks 4student-marks 5bookdetails 1bookdetails 2bookdetails 3bookdetails 4bookdetails 5
In the above example, instead of awk FNR, if you use awk NR, for the file bookdetails the you will get from 6 to 10 for each record.