Professional Documents
Culture Documents
Output:
Jones 2143 78 84 77 => Pass
Gondrol 2321 56 58 45 => Pass
RinRao 2122 38 37 => Fail
Edwin 2537 87 97 95 => Pass
Dayan 2415 30 47 => Fail
Awk If Else If ladder
Syntax: if(conditional-expression1)
action1;
else if(conditional-expression2)
action2;
else if(conditional-expression3)
action3;
.
.
else
action n;
• If the conditional-expression1 is true then action1 will be performed.
• If the conditional-expression1 is false then conditional-expression2 will be
checked, if its true, action2 will be performed and goes on like this.
• Last else part will be performed if none of the conditional-expression is
true.
Awk If Else If Example: Find the average and grade for every student
Vi grade.awk
BEGIN{ }
{
total=$3+$4+$5;
avg=total/3;
if ( avg >= 90 )
grade="A";
else if ( avg >= 80)
grade ="B";
else if (avg >= 70)
grade ="C";
else grade="D";
print $0,"=>",grade;
}
END{ }
Run : $ awk -f grade.awk student-marks
Output:
• Jones 2143 78 84 77 => C
• Gondrol 2321 56 58 45 => D
• RinRao 2122 38 37 => D
• Edwin 2537 87 97 95 => A
• Dayan 2415 30 47 => D
awk loopstatements
• let us review about awk loop statements – while, do while,
for loops, break, continue, and exit statements .
• Awk looping statements are used for performing set of
actions again and again in succession.
• It repeatedly executes a statement as long as condition is
true.
• Awk has number of looping statement as like ‘C’
programming language.
Awk While Loop
Syntax:
while(condition)
actions
• while is a keyword.
• condition is conditional expression
• actions are body of the while loop which can have one or more statement.
• If actions has more than one statement, it has to be enclosed with in the
curly braces.
How it works?
• Awk while loop checks the condition first, if the condition is true, then it
executes the list of actions.
• After action execution has been completed, condition is checked again, and
if it is true, actions is performed again.
• This process repeats until condition becomes false.
• If the condition returns false in the first iteration then actions are never
executed.
Example :
$ cat >employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
Write a awk script to prints the first three fields of each record, one per line.
BEGIN{}
{
i=1;
while (i <= 3)
{
print $i;
i++;
}
}
END{}
Output :
RUN : $awk -f awhile.awk employee.txt
100
Thomas
Manager
200
Jason
Developer
300
Sanjay
Sysadmin
400
Nisha
Manager
500
Randy
DBA
Example: Write an awk script to reverse a number::
$cat >awhile2/*create a file */
342
134
Vi awhile2.awk /*awk script
BEGIN{}
{
no=$1
rem=0
while (no >1)
{
rem =no%10
no/=10
printf "%d",rem
}
printf "\n"
}
END{}
Awk Do-While Loop
How it works? –
• Awk Do while loop is called exit controlled loop, whereas
awk while loop is called as entry controlled loop.
• Because while loop checks the condition first, then it
decides to execute the body or not.
• But the awk do while loop executes the body once, then
repeats the body as long as the condition is true.
Syntax:
do
action
while(condition)
Example : To prints each input record 2 times by using do while ::
BEGIN{ }
{
i=1
do
{
print $0
i++
} while (i <=3 )
}
END{ }
Output:
100 Thomas Manager Sales $5,000
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
500 Randy DBA Technology $6,000
Awk For Loop Statement
• Awk for statement is same as awk while loop, but it is syntax is much
easier to use.
Syntax:
for(initialization ; condition ; increment/decrement)
Actions
How it works? —
• Awk for statement starts by executing initialization, then checks the
condition, if the condition is true, it executes the actions, then
increment or decrement.
• Then as long as the condition is true, it repeatedly executes action
and then increment/decrement.
Awk For Loop Example :: Print the sum of fields in all lines.
$cat >awfor /*create a file
1 2
3 4
Vi awfor.awk /*awk script*/
BEGIN{}
{
for (i=1;i <= NF;i++)
total=total+$i;
}
END{ print total}
RUN:
$ awk -f awfor.awk awfor
10