Perfected Unix and AWK Guide
Perfected Unix and AWK Guide
Example: ls -l
CD
Changes the current working directory.
Example: cd /home/user
MKDIR
Creates a new directory.
RM
Removes files or directories.
Example: rm file.txt
TOUCH
Creates empty files.
CAT
Displays file contents.
NANO/VI
Edits files in terminal.
CHMOD
Changes file permissions.
Example: chmod 755 script.sh
CHOWN
Changes file ownership.
PS
Shows running processes.
Example: ps aux
TOP
Displays real-time process list.
Example: top
KILL
Terminates a process.
GREP
Searches for patterns.
FIND
Searches for files.
SORT
Sorts lines in a file.
UNIQ
Filters repeated lines.
CUT
Extracts specific fields.
HEAD
Shows beginning lines.
TAIL
Shows ending lines.
ECHO
Displays a string.
REDIRECTION
Redirects output.
PIPE
Connects commands.
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:
awk 'BEGIN{FS=" ";}
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
}'
--------------------------------------------------------------------------------------
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: To Search for a word and to avoid it to match the substrings -w option is used
Situation: If you want to display the lines which does not matches the given string/pattern then
-----------------------------------------------------------------------------------------------------------
SED COMMAND
file_name
3.Hardware
4.Security (Firewall, Network etc.)
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
Regular Expressions
DOT
EXCLUSIVE SET
ALTERATION
GROUPING
----------------------------------------------------------------------------------
return num1
return num2
return num1
return num2
# Main function
BEGIN {
main(10, 20)
--------------------------------------------------------------------------------
IF STATEMENT
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 Exercise Solutions with Detailed Explanations
Q1: Remove the header and print names
Command:
Explanation:
Skips the first line (header) using NR > 1, and prints the 2nd field (NAME).
Explanation:
Prints the NAME field and pipes it to sort for alphabetical order.
Explanation:
awk -F@ 'NR > 1 && $5 > 60000 { print $2 "@" $5 }'
Explanation:
Selects rows with salary > 60000, prints name and salary separated by '@'.
awk -F@ 'NR > 1 && $4=="HR" { count++ } END { print count }'
Explanation:
Increments a counter for HR department rows and prints count in the END block.
Explanation:
Explanation:
awk -F@ 'NR > 1 && $4=="Marketing" { sum+=$5; count++ } END { print sum/count }'
Explanation:
awk -F@ 'NR == 2 || $5 < min { min=$5; name=$2 } END { print name, min }'
Explanation:
Tracks lowest salary from second line onwards, stores corresponding name.
awk -F@ 'NR > 1 { inc = $5 * 1.10; print $2, inc }'
Explanation:
Calculates new salary by multiplying with 1.10 and prints with name.
Explanation:
Prints all rows except header, sorted numerically by salary column.
Explanation:
Explanation:
Explanation:
Explanation:
Explanation: