Unix and AWK Guide Final
Unix and AWK Guide Final
fruit_id,fruit_name,fruit_qty,unit_price,total_price
1,Mango,2,10,20
2,Apple,6,15,90
3,Banana,4,8,int
4,Watermelon,7,9,63
5,apple,3,15,45
cat fruits.txt
1,Mango,2,10,20
2,Apple,6,15,90
Mango
Apple
Banana...
2,Apple,6,15,90
5,apple,3,15,45
head -n 3 fruits.txt
wc -l fruits.txt
uniq
tail -n 2 fruits.txt
Command:
Explanation:
Skips header (NR > 1) and prints the 2nd field (Name).
Example:
Command:
Unix and AWK Guide (With Examples)
Explanation:
Example:
Command:
awk -F@ 'NR > 1 && $4=="Sales" { print $2 }' employees.txt | sort -r
Explanation:
Selects only Sales department employees and sorts names in reverse order.
Example:
Command:
awk -F@ 'NR > 1 && $5 > 60000 { print $2 "@" $5 }' employees.txt
Explanation:
Example:
Command:
Unix and AWK Guide (With Examples)
awk -F@ 'NR > 1 && $4=="HR" { count++ } END { print count }' employees.txt
Explanation:
Example:
Command:
awk -F@ 'NR > 1 { sum += $5 } END { print sum }' employees.txt
Explanation:
Example:
Command:
awk -F@ 'NR > 1 && $3 > 40 { print $2, $3 }' employees.txt
Explanation:
Example:
Command:
Unix and AWK Guide (With Examples)
awk -F@ 'NR > 1 && $4=="Marketing" { sum+=$5; count++ } END { print sum/count }' employees.txt
Explanation:
Example:
Command:
awk -F@ 'NR == 2 || $5 < min { min=$5; name=$2 } END { print name, min }' employees.txt
Explanation:
Example:
Command:
awk -F@ 'NR > 1 { inc = $5 * 1.10; print $2, inc }' employees.txt
Explanation:
Example:
Command:
Unix and AWK Guide (With Examples)
Explanation:
Example:
Command:
Explanation:
Example:
Command:
Explanation:
The '^' symbol matches beginning of string; selects names starting with 'S'.
Example:
Command:
Unix and AWK Guide (With Examples)
Explanation:
The '$' symbol matches end of string; selects names ending with 'a'.
Example:
Command:
Explanation:
Example:
Command:
Explanation:
Example:
fruit_id,fruit_name,fruit_qty,unit_price,total_price
1,Mango,2,10,20
2,Apple,6,15,90
3,Banana,4,8,int
4,Watermelon,7,9,63
Solution:
awk 'BEGIN{
FS=",";
OFS="|";
print $2,$3,$5;
}' fruits.txt
--------------------------------------------------------------------------------------
Example 2: To Find the total price of an apple (PIPELINING grep & awk)
fruit_id,fruit_name,fruit_qty,unit_price,total_price
1,Mango,2,10,20
2,Apple,6,15,90
3,Banana,4,8,int
4,Watermelon,7,9,63
5,apple,3,15,45
Solution:
{
Unix and AWK Guide (With Examples)
s = s+$5;
END{
}'
--------------------------------------------------------------------------------------
Solution:
awk 'BEGIN{
i=1;
while(i<=10)
i++;
}'
--------------------------------------------------------------------------------------
Example 4: Find the number of fields in a record and total no.of records present in the text file
mango
watermelon
pine apple
custard apple
banana
Solution:
Unix and AWK Guide (With Examples)
print "The Number of fields present in record " NR " is " NF".";
END{
}' fruits2.txt
---------------------------------------------------------------------------------------------
Input Data:
29,Arun
26,Karthik
28,Kiran
52,Raju
78,Rachel
Example 5:
awk 'BEGIN{FS=",";}
if($1>50)
else
i=1;
Unix and AWK Guide (With Examples)
while(i<=1)
print "Row " NR ", Column 2 (loop "i "): " $2;
i++;
END{
}' example.txt
Example 6:
total_age += $1;
count++;
END{
if(count>0)
avg_age = total_age/count;
else
}'
--------------------------------------------------------------------------------------
Unix and AWK Guide (With Examples)
employeeDetails.txt (DATA) :
Name,Age,Place,Experience,Salary
Anish,26,Chennai,2,10000
Jai,24,Chennai,2,10000
Kumar,29,Hyderabad,5,32000
John,32,Mumbai,2,11000
Neethu,21,Nagpur,3,13000
Satish,22,Ahmedabad,2,10000
awk 'BEGIN{FS=",";}
print;
}' employeeDetails.txt
Situation: Manager wish to display the employee name and salary working in royal mail hotel
awk 'BEGIN{FS=",";}
print $1,$5;
}' employeeDetails.txt
Situation: Manager wishes to find the total expenses of hotel per month in the form of salary
Unix and AWK Guide (With Examples)
s=0;
s=s+$5;
END{
}' employeeDetails.tx