Using NR Variable in awk with Relational Operators
NR (Number of Record) can be used with all relational operators to filter records in a file. Below are some examples:
Examples
-
Skip the First Two Records:
awk 'NR>2 {print $0}' FS="," emp.csv
-
Print the First Field of Records After the First Two:
awk 'NR>2 {print $1}' FS="," emp.csv
-
Display Only the 2nd and 4th Records:
awk 'NR==2 || NR==4 {print $0}' FS="," emp.csv
Output: Displays the full content of the 2nd and 4th records.
-
Display the First Field of the 2nd and 4th Records:
awk 'NR==2 || NR==4 {print $1}' FS="," emp.csv
Output: Displays only the first field of the 2nd and 4th records.
-
Display the 3rd and 4th Records:
awk 'NR>2 && NR<5 {print $0}' FS="," emp.csv
-
Display Records from the 2nd to the 4th:
awk 'NR>1 && NR<5 {print $0}' FS="," emp.csv
-
Display Records Between the 2nd and 4th (Inclusive):
awk 'NR==2, NR==4 {print $0}' FS="," emp.csv
Output: Displays records from the 2nd to the 4th.
Explanation of $0, $1, $2, ... in awk
In awk, each line of a file is treated as a record, and fields within a record are separated by the Field Separator (FS, default is whitespace).
$0: Refers to the entire record (the full line).$1: Refers to the first field.$2,$3, and so on refer to the respective fields.
Example Data (File: emp.csv)
100,Durga,1000,Mumbai
200,Bunny,2000,Hyderabad
300,Chinny,3000,Hyderabad
400,Vinny,4000,Chennai
500,Pinny,5000,MumbaiUsing awk with Examples
-
Print the Entire Line (
$0):awk '{print $0}' FS="," emp.csv
Output:
100,Durga,1000,Mumbai 200,Bunny,2000,Hyderabad 300,Chinny,3000,Hyderabad 400,Vinny,4000,Chennai 500,Pinny,5000,Mumbai -
Print the First Field (
$1):awk '{print $1}' FS="," emp.csv
Output:
100 200 300 400 500 -
Print the Second Field (
$2):awk '{print $2}' FS="," emp.csv
Output:
Durga Bunny Chinny Vinny Pinny -
Print the Third Field (
$3):awk '{print $3}' FS="," emp.csv
Output:
1000 2000 3000 4000 5000 -
Print the Fourth Field (
$4):awk '{print $4}' FS="," emp.csv
Output:
Mumbai Hyderabad Hyderabad Chennai Mumbai
Advanced Examples
-
Print Specific Fields (
$1and$2):awk '{print $1, $2}' FS="," emp.csv
Output:
100 Durga 200 Bunny 300 Chinny 400 Vinny 500 Pinny -
Print Fields in a Custom Format:
awk '{print "ID:", $1, "Name:", $2, "Salary:", $3, "City:", $4}' FS="," emp.csv
Output:
ID: 100 Name: Durga Salary: 1000 City: Mumbai ID: 200 Name: Bunny Salary: 2000 City: Hyderabad ID: 300 Name: Chinny Salary: 3000 City: Hyderabad ID: 400 Name: Vinny Salary: 4000 City: Chennai ID: 500 Name: Pinny Salary: 5000 City: Mumbai -
Print Only the Last Field (
$NF):awk '{print $NF}' FS="," emp.csv
Output:
Mumbai Hyderabad Hyderabad Chennai Mumbai -
Print All Fields Except the Last (
$(NF-1)):awk '{for (i=1; i<NF; i++) printf $i ","; print ""}' FS="," emp.csv
Output:
100,Durga,1000, 200,Bunny,2000, 300,Chinny,3000, 400,Vinny,4000, 500,Pinny,5000,
Key Points
$0: Entire record (full line).$1: First field.$NF: Last field.NF: Total number of fields in the current record.
Q) Consider the File: xy.txt
1.abc
2.def
3.ghi
4.jkl5.mno
What is the output of the following commands?
-
Command:
awk '{print $0;}' xy.txtExplanation: It will print the entire file as it is.
Output:1.abc 2.def 3.ghi 4.jkl5.mno -
Command:
awk '{print $1;}' xy.txtExplanation: It will print the entire file because the file contains only one column.
Output:1.abc 2.def 3.ghi 4.jkl5.mno -
Command:
awk '{print $2;}' xy.txtExplanation: Nothing will be printed because there is no second column in the file.
Output: (No Output)
AWK Code into a Separate File
We can separate AWK code into a file and use the -f option to execute it.
Example
AWK Code File (x.txt):
BEGIN {print "BEGIN"}
{print $0}
END {print "END"}Command:
awk -f x.txt FS="," emp.csvOutput:
BEGIN
eno,ename,esal,eaddr
100,Sunny,1000,Mumbai
200,Bunny,2000,Hyderabad
300,Chinny,3000,Hyderabad
400,Vinny,4000,Chennai
500,Pinny,5000,Mumbai
END
Filter Data Based on Conditions
-
List all employees where
eaddris Hyderabad:awk '$4=="Hyderabad"{print $0}' FS="," emp.csv
Output:
200,Bunny,2000,Hyderabad 300,Chinny,3000,Hyderabad -
List all employees where
esalis greater than 2500:awk '$3>2500{print $0}' FS="," emp.csv
Output:
eno,ename,esal,eaddr 300,Chinny,3000,Hyderabad 400,Vinny,4000,Chennai 500,Pinny,5000,Mumbai -
Skip the header and filter records where
esalis greater than 2500:awk 'NR!=1 && $3>2500{print $0}' FS="," emp.csv
Output:
300,Chinny,3000,Hyderabad 400,Vinny,4000,Chennai 500,Pinny,5000,Mumbai
Search Records Using a String
-
Filter all records containing
20in any field:awk '/20/{print $0}' FS="," emp.csv
Output:
200,Bunny,2000,Hyderabad 20,Durga,5000,Hyderabad 50,Shiva,2020,Hyderabad -
Filter records where the 3rd field contains
20:awk '$3~/20/{print $0}' FS="," emp.csv
Output:
200,Bunny,2000,Hyderabad 50,Shiva,2020,Hyderabad -
Exclude records where the 3rd field contains
20:awk '$3!~/20/{print $0}' FS="," emp.csv
Output:
eno,ename,esal,eaddr 100,Sunny,1000,Mumbai 300,Chinny,3000,Hyderabad 400,Vinny,4000,Chennai 500,Pinny,5000,Mumbai
Key Notes
- AWK treats every file as a tabular file by default.
- Use
FSto specify the field separator (default is whitespace). - Search strings can be applied globally or to specific fields using conditional expressions.
Match Total Field Value
-
Match any field containing
2000:awk '/2000/{print $0}' FS="," emp.csv
-
Match if the 3rd field contains
2000:awk '$3~/2000/{print $0}' FS="," emp.csv
-
Match if the 3rd field value is exactly
2000:awk '$3==2000{print $0}' FS="," emp.csv
-
Match if the 4th field value is exactly
Hyderabad:awk '$4=="Hyderabad"{print $0}' FS="," emp.csv
Increment Salary by 500 Where Salary is Less Than 3000
-
Print records where salary is less than 3000:
awk -F "," '{if($3<3000) print $0}' emp.csv
-
Increment salary by 500 where salary is less than 3000:
awk -F "," '{if($3<3000) {$3=$3+500;print $0}}' emp.csv
Count Employees with Salary Greater Than 3000
- Command:
awk -F "," 'BEGIN{c=0}NR!=1{if($3>3000){c=c+1;print $0;}}END{print "The Total Number of Employees where salary > 3000 :" c}' emp.csv
Another Way to Count Employees
AWK Code File (x.txt):
BEGIN {
FS=",";
c=0;
}
NR!=1 {
if($3>3000) {
c=c+1;
print $0;
}
}
END {
print "The Total Number of Employees where salary > 3000 :" c;
}Command:
awk -f x.txt emp.csv