Sodapdf Converted
Sodapdf Converted
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:
--------------------------------------------------------------------------------------
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 no.of records present in the text file
mango
watermelon
pine apple
custard apple
banana
Solution:
---------------------------------------------------------------------------------------------
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;
while(i<=1)
{
print "Row " NR ", Column 2 (loop "i "): " $2;
i++;
}
}
END{
print match("End of Code", /of/);
print RSTART, RLENGTH;
}’ example.txt
Example 6: Average
grep ’,A’ input3.txt | 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";
}
}’
Data
A|T|X|G
G|X|T|A
G|T|A|X
Solution:
awk ’BEGIN{OFS=FS="|";}
{
if($2=="T")
{
sub($1,"T");
print;
}
}END{
}
’ data.txt
Data:
Solution:
awk ’{
for (i=1;i<=NF;i++)
{
if ($i<=1000){
next;
}
};
print;
}’ < data.txt
--------------------------------------------------------------------------------------
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
Situation: Manager wishes to find the total no.of employees earning 10000 per month
awk ’/10000/{
++count;
}
END{
print "No.of employees earning 10000 per month is " count;
}’ employeeDetails.txt
Situation: Manager wishes to find the employees as best performers who completed 2 years of
exprerience and earning more than 10000
awk ’BEGIN{
FS=",";
print "*********************Performance Report*********************";
}
{
if(NR!=1)
{
if($4>=2 && $5>10000)
{
print $1" is a good performer";
}
else if($4==2 && $5<=10000)
{
print $1" Needs to improve";
}
}
}
END{
print "*********************Performance Report*********************";
}’ employeeDetails.txt
-----------------------------------------------------------------------------------------
SORT COMMAND
Situation: Sort on the 2nd field of file named "list". File list is comma seperated value
Solution: sort -t"," -k 2 list.txt
Situation: Sort in reverse order of first numeric column from multiple files
EXAMPLE SOLUTION:
---------------------------------------------------------------------------------------------------------
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
-n = suppress automatic printing of patternspace. It will not print any thing until explicit request to print is
found
-d = delete the line when pattren matches
-i = modifies the text in the input file
SYNTAX:
Example: The search for "Unix" to replace with "Unix OS" should be case-insensitive.
INPUT:
If the input file contains the following lines
Unix is an multi-user,multi-tasking system.
It is a command based operating system.
We will learn unix in this module.
------------------------------------------------------------------------------ Pipelining
------------------------------------------------------------------
Write a unix command to find the total price of assests belonging to finance department
Input:
AssetName,AssetType,Department,AssetPrice
lap1,laptop,finance,60000
des1,desktop,FinancE,40000
lap2,laptop,FINANCE,50000
des2,desktop,inance,80000
prn1,printer,finan,10000
prn2,printer,Finances,20000
Output:
Solution:
------------------------------------------------------------------------------AWK Command-------------------------------------
Regular Expressions
DOT
EXCLUSIVE SET
echo -e "Call\nTall\nBall" | awk ’/[^CT]all/’
ALTERATION
GROUPING
----------------------------------------------------------------------------------
# Main function
function main(num1, num2){
# Find minimum number
result = find_min(10, 20)
print "Minimum =", result
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)
printf "%d is even number.\n", num;
else
printf "%d is odd number.\n", num;
}’
IF-ELSE-IF LADDER
awk ’BEGIN {
a = 30;
if (a==10)
print "a = 10";
else if (a == 20)
print "a = 20";
else if (a == 30)
print "a = 30";
}’
---------------------------------------------------------------------------
HACKER RANK
awk ’BEGIN{
# Loop through numbers from 1 to 99
for(i=1;i<=99;i++)
{
# Check if the number is odd (remainder when divided by 2 is not equal to 0)
if(i%2 != 0)
{
print i;
}
}
}’
Script which accepts name as input and displays the greeting "Welcome(name)"
read name;
echo "Welcome $name";
Use a for loop to display the natural numbers from 1 to 50
awk ’BEGIN{
for(i=1;i<=50;i++)
{
if(i/2 != 0)
{
print i;
}
}
}’
Given two integers, X and Y , find their sum, difference, product, and quotient.
read a;
read b;
awk ’BEGIN{FS=",";}
{
X=$1;Y=$2;
if(X > Y)
print "X is greater than Y";
else if(X < Y)
print "X is less than Y";
else
print "X is equal to Y";
}’ input.txt