Complete Unix and AWK Guide
Complete Unix and AWK Guide
This section uses real datasets and output examples to demonstrate core Unix utilities like cat, grep, sort,
fruit_id,fruit_name,fruit_qty,unit_price,total_price
1,Mango,2,10,20
2,Apple,6,15,90
3,Banana,4,8,32
4,Watermelon,7,9,63
5,apple,3,15,45
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:
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:
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;
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
}'
--------------------------------------------------------------------------------------
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
s=0;
s=s+$5;
END{
}' employeeDetails.txt
Situation: Manager wishes to find the total no.of employees earning 10000 per month
awk '/10000/{
++count;
END{
}' employeeDetails.txt
Situation: Manager wishes to find the employees as best performers who completed 2 years of exprerience
awk 'BEGIN{
FS=",";
if(NR!=1)
END{
}' employeeDetails.txt
-----------------------------------------------------------------------------------------
SORT COMMAND
Situation: Sort on the 2nd field of file named "list". File list is comma seperated value
Situation: Sort in reverse order of first numeric column from multiple files
Situation: Sort the above input file by removing the duplicate records/lines.
---------------------------------------------------------------------------------------------------------
UNIQ COMMAND
------------------------------------------------------------------------------------------
GREP COMMAND
demo_file
Situation: Search for a given string and also you can check a string in multiple files
Situation: Search for a line starting with 'Two' and ending with 'empty'
Situation: If you want to display the lines which does not matches the given string/pattern then
-----------------------------------------------------------------------------------------------------------
SED COMMAND
file_name
3.Hardware
5.Storage
Operation in Sed
-n = suppress automatic printing of patternspace. It will not print any thing until explicit request to print is
found
SYNTAX:
Prints the lines containing 'Security' until explicit request to print is found
------------------------------------------------------------------------------AWK Command-------------------------------------
Regular Expressions
DOT
EXCLUSIVE SET
ALTERATION
GROUPING
----------------------------------------------------------------------------------
return num1
return num2
return num1
return num2
# Main function
BEGIN {
main(10, 20)
--------------------------------------------------------------------------------
awk 'BEGIN {num = 10; if (num % 2 == 0) printf "%d is even number.\n", num }'
IF-ELSE STATEMENT
awk 'BEGIN {
num = 11;
if (num % 2 == 0)
else
}'
IF-ELSE-IF LADDER
awk 'BEGIN {
a = 30;
if (a==10)
else if (a == 20)
else if (a == 30)
}'
3. AWK Command Exercises - Solutions and Explanations
Q1: Display names, skip header
Command:
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
awk -F@ 'NR > 1 && $5 > 60000 { print $2 "@" $5 }'
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
awk -F@ 'NR > 1 && $4=="HR" { count++ } END { print count }'
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
awk -F@ 'NR > 1 && $4=="Marketing" { sum+=$5; count++ } END { print sum/count }'
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
awk -F@ 'NR == 2 || $5 < min { min=$5; name=$2 } END { print name, min }'
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
awk -F@ 'NR > 1 { inc = $5 * 1.10; print $2, inc }'
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000
Explanation:
This command uses '@' as the field separator and applies a condition to extract specific information.
Example: 1@Sanjay@25@Sales@50000