Unix and AWK Guide (With Examples)
1. Unix Basics with Real Dataset Examples
We use the following sample data ([Link]):
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 [Link]
Displays file content:
1,Mango,2,10,20
2,Apple,6,15,90
cut -d',' -f2 [Link]
Extracts 2nd column (fruit_name):
Mango
Apple
Banana...
grep -i 'apple' [Link]
Finds all lines with 'apple' (case-insensitive):
2,Apple,6,15,90
5,apple,3,15,45
awk -F',' '{ print $2, $5 }' [Link]
Prints 2nd and 5th columns: fruit_name and total_price.
Unix and AWK Guide (With Examples)
sort -t',' -k5,5n [Link]
Sorts based on total_price numerically.
head -n 3 [Link]
Displays the first 3 lines of the file.
wc -l [Link]
Counts the number of lines (records).
uniq
Filters duplicates (works on sorted data).
tail -n 2 [Link]
Shows the last two lines.
2. AWK Command Programs Explained
Q1: Display all employee names
Command:
awk -F@ 'NR > 1 { print $2 }' [Link]
Explanation:
Skips header (NR > 1) and prints the 2nd field (Name).
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q2: Sort employee names in ascending order
Command:
Unix and AWK Guide (With Examples)
awk -F@ 'NR > 1 { print $2 }' [Link] | sort
Explanation:
Prints names and sorts them alphabetically using sort.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q3: Sales employees, sorted descending
Command:
awk -F@ 'NR > 1 && $4=="Sales" { print $2 }' [Link] | sort -r
Explanation:
Selects only Sales department employees and sorts names in reverse order.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q4: Salary > 60000, print Name and Salary
Command:
awk -F@ 'NR > 1 && $5 > 60000 { print $2 "@" $5 }' [Link]
Explanation:
Prints name and salary if salary is greater than 60000.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q5: Count HR employees
Command:
Unix and AWK Guide (With Examples)
awk -F@ 'NR > 1 && $4=="HR" { count++ } END { print count }' [Link]
Explanation:
Counts number of HR employees.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q6: Total salary of all employees
Command:
awk -F@ 'NR > 1 { sum += $5 } END { print sum }' [Link]
Explanation:
Adds up salaries and prints total.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q7: Names and ages of employees older than 40
Command:
awk -F@ 'NR > 1 && $3 > 40 { print $2, $3 }' [Link]
Explanation:
Checks if age > 40 and prints name and age.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q8: Average salary in Marketing
Command:
Unix and AWK Guide (With Examples)
awk -F@ 'NR > 1 && $4=="Marketing" { sum+=$5; count++ } END { print sum/count }' [Link]
Explanation:
Computes average salary for Marketing department.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q9: Minimum salary - print Name and Salary
Command:
awk -F@ 'NR == 2 || $5 < min { min=$5; name=$2 } END { print name, min }' [Link]
Explanation:
Tracks minimum salary and corresponding name.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q10: Salary incremented by 10%
Command:
awk -F@ 'NR > 1 { inc = $5 * 1.10; print $2, inc }' [Link]
Explanation:
Calculates 10% raise and prints updated salary.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q11: Sort by salary (ascending)
Command:
Unix and AWK Guide (With Examples)
awk -F@ 'NR > 1' [Link] | sort -t@ -k5,5n
Explanation:
Sorts whole lines based on 5th field (Salary).
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q12: Display first 3 rows (excluding header)
Command:
awk -F@ 'NR > 1 && NR <= 4' [Link]
Explanation:
Prints 3 employee lines after skipping header.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q13: Names starting with S
Command:
awk -F@ 'NR > 1 && $2 ~ /^S/ { print $2 }' [Link]
Explanation:
The '^' symbol matches beginning of string; selects names starting with 'S'.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q14: Names ending with a
Command:
Unix and AWK Guide (With Examples)
awk -F@ 'NR > 1 && $2 ~ /a$/ { print $2 }' [Link]
Explanation:
The '$' symbol matches end of string; selects names ending with 'a'.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q15: Names containing 'y'
Command:
awk -F@ 'NR > 1 && $2 ~ /y/ { print $2 }' [Link]
Explanation:
Matches names containing the letter 'y'.
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
Q16: Names containing vowels
Command:
awk -F@ 'NR > 1 && $2 ~ /[aeiouAEIOU]/ { print $2 }' [Link]
Explanation:
Matches names that contain at least one vowel (a, e, i, o, u).
Example:
Suppose a line is: 1@Sanjay@25@Sales@50000 -> $2 is 'Sanjay', $4 is 'Sales', $5 is '50000'
3. Additional AWK Programs from Unix 1 (1).txt
Example 1: To Print the 2nd, 3rd and 5th Columns only
Unix and AWK Guide (With Examples)
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;
}' [Link]
--------------------------------------------------------------------------------------
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:
grep -i "[Aa]pple" [Link] | awk 'BEGIN{FS=",";s=0;}
{
Unix and AWK Guide (With Examples)
s = s+$5;
END{
print "The total price spent on apple fruit is " s;
}'
--------------------------------------------------------------------------------------
Example 3: Printing the Multiplication of Number 5
Solution:
awk 'BEGIN{
i=1;
while(i<=10)
print "5 * "i" = "i*5;
i++;
}'
--------------------------------------------------------------------------------------
Example 4: Find the number of fields in a record and total [Link] records present in the text file
mango
watermelon
pine apple
custard apple
banana
Solution:
Unix and AWK Guide (With Examples)
awk 'BEGIN{FS=" ";}
print "The Number of fields present in record " NR " is " NF".";
END{
print NR" records are present.";
}' [Link]
---------------------------------------------------------------------------------------------
Input Data:
29,Arun
26,Karthik
28,Kiran
52,Raju
78,Rachel
Example 5:
awk 'BEGIN{FS=",";}
if($1>50)
print "Value is greater than 50.";
else
print "Value is less than 50";
i=1;
Unix and AWK Guide (With Examples)
while(i<=1)
print "Row " NR ", Column 2 (loop "i "): " $2;
i++;
END{
print match("End of Code", /of/);
print RSTART, RLENGTH;
}' [Link]
Example 6:
grep ',A' [Link] | awk 'BEGIN{FS=",";total_age=0;}
total_age += $1;
count++;
END{
if(count>0)
avg_age = total_age/count;
printf "Average Age for Group A:%.2f\n", avg_age;
else
print "No Data Found";
}'
--------------------------------------------------------------------------------------
Unix and AWK Guide (With Examples)
ROYAL MAIL HOTEL (CASE STUDY)
[Link] (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
Situation: To Print the complete data
awk 'BEGIN{FS=",";}
print;
}' [Link]
Situation: Manager wish to display the employee name and salary working in royal mail hotel
awk 'BEGIN{FS=",";}
print $1,$5;
}' [Link]
Situation: Manager wishes to print details of Kumar and Satish
awk '/Kumar|Satish/' [Link]
Situation: Manager wishes to find the total expenses of hotel per month in the form of salary
Unix and AWK Guide (With Examples)
awk -F"," 'BEGIN{
s=0;
s=s+$5;
END{
print "Total Exprenses per month in form of salary is " s;
}' [Link]