AWK and SORT Exam Preparation Questions
(Linux Administrator Level)
Sample Dataset ([Link]):
BookingID,CustomerName,City,Amount,Date
101,Ankit,Delhi,5000,2026-01-10
205,Rahul,Mumbai,7000,2026-02-01
150,Anita,Pune,6500,2026-01-25
120,Amit,Delhi,4000,2026-01-15
300,Sneha,Chennai,9000,2026-02-10
250,Rohit,Bangalore,8000,2026-02-05
180,Neha,Delhi,7200,2026-01-30
Questions
Question 1: Display all records except the header.
Question 2: Display only CustomerName and Amount.
Question 3: Display records where CustomerName starts with 'A'.
Question 4: Display records where Amount is greater than 7000.
Question 5: Display total number of records.
Question 6: Display sum of all Amount.
Question 7: Display records sorted by BookingID ascending.
Question 8: Display records sorted by BookingID descending.
Question 9: Display only unique City names.
Question 10: Display records where City is Delhi.
Question 11: Display BookingID and CustomerName sorted by Amount descending.
Question 12: Display the highest Amount.
Question 13: Display the lowest Amount.
Question 14: Display records where BookingID is between 150 and 250.
Question 15: Display number of customers in each City.
Question 16: Display records sorted by CustomerName alphabetically.
Question 17: Display BookingID, CustomerName, Date sorted by BookingID descending.
Question 18: Display records where Amount is less than 6000.
Question 19: Display the average Amount.
Question 20: Display records sorted by City and then Amount descending.
Answers with Explanation
Answer 1:
awk 'NR>1' file
Explanation: NR>1 skips header line.
Answer 2:
awk -F',' '{print $2, $4}' file
Explanation: Extracts specific columns using field separator.
Answer 3:
awk -F',' '$2~/^A/' file
Explanation: Regex matches names starting with A.
Answer 4:
awk -F',' '$4>7000' file
Explanation: Numeric comparison on Amount column.
Answer 5:
awk 'END{print NR-1}' file
Explanation: NR counts total lines including header.
Answer 6:
awk -F',' '{sum+=$4} END{print sum}' file
Explanation: Adds all Amount values.
Answer 7:
sort -t',' -k1,1n file
Explanation: Sort numerically by BookingID ascending.
Answer 8:
sort -t',' -k1,1nr file
Explanation: Sort numerically by BookingID descending.
Answer 9:
awk -F',' '{print $3}' file | sort -u
Explanation: Unique cities.
Answer 10:
awk -F',' '$3=="Delhi"' file
Explanation: Condition filter.
Answer 11:
awk -F',' '{print $1,$2,$4}' file | sort -k3,3nr
Explanation: Sort by Amount descending.
Answer 12:
sort -t',' -k4,4nr file | head -1
Explanation: Highest Amount first.
Answer 13:
sort -t',' -k4,4n file | head -1
Explanation: Lowest Amount first.
Answer 14:
awk -F',' '$1>=150 && $1<=250' file
Explanation: Range condition.
Answer 15:
awk -F',' '{count[$3]++} END{for(i in count) print i,count[i]}' file
Explanation: Associative array counting.
Answer 16:
sort -t',' -k2,2 file
Explanation: Sort alphabetically.
Answer 17:
awk -F',' '{print $1,$2,$5}' file | sort -k1,1nr
Explanation: Sort by BookingID descending.
Answer 18:
awk -F',' '$4<6000' file
Explanation: Amount condition.
Answer 19:
awk -F',' '{sum+=$4} END{print sum/(NR-1)}' file
Explanation: Average calculation.
Answer 20:
sort -t',' -k3,3 -k4,4nr file
Explanation: Sort by City then Amount descending.