This Blog is intended to collect information of my various Intrests,pen my opinion on the information gathered and not intended to educate any one of the information posted,but are most welcome to share there view on them

Friday, January 20, 2012

Variable which defines values which can be changed such as field separator and record separator.

Variable which can be used for processing and reports such as Number of records, number of fields.

1. Awk FS Example: Input field separator variable.

Awk reads and parses each line from input based on whitespace character by default and set the variables $1,$2 and etc. Awk FS variable is used to set the field separator for each record. Awk FS can be set to any single character or regular expression. You can use input field separator using one of the following two options:

Awk FS is any single character or regular expression which you want to use as a input field separator.

Awk FS can be changed any number of times, it retains its values until it is explicitly changed. If you want to change the field separator, its better to change before you read the line. So that change affects the line what you read.

Here is an awk FS example to read the /etc/passwd file which has “:” as field delimiter.

Concatenator in the print statement “,” concatenates two parameters with a space which is the value of awk OFS by default. So, Awk OFS value will be inserted between fields in the output as shown below.

In the script student.awk, it reads each student detail as a single record,because awk RS has been assigned to double new line character and each line in a record is a field, since FS is newline character.

4. Awk ORS Example: Output Record Separator Variable

Awk ORS is an Output equivalent of RS. Each record in the output will be printed with this delimiter. Following is an awk ORS example:

In the above script,each records in the file student-marks file is delimited by the character “=”.

5. Awk NR Example: Number of Records Variable

Awk NR gives you the total number of records being processed or line number. In the following awk NR example, NR variable has line number, in the END section awk NR tells you the total number of records in a file.

Like any other programming language Awk also has lot of operators for number and string operations. In this article let us discuss about all the key awk operators.
There are two types of operators in Awk.

Unary Operator – Operator which accepts single operand is called unary operator.

Binary Operator – Operator which accepts more than one operand is called binary operator.

Awk Unary Operator

Operator

Description

+

Positivate the number

-

Negate the number

++

AutoIncrement

–

AutoDecrement

Awk Binary Operator

There are different kinds of binary operators are available in Awk. It is been classified based on its usage.

Awk Arithmetic Opertors

The following operators are used for performing arithmetic calculations.

Awk Example 1: Count the total number of fields in a file.

The below awk script, matches all the lines and keeps adding the number of fields in each line,using shortcut addition assignment operator. The number of fields seen so far is kept in a variable named ‘total’. Once the input has been processed, special pattern ‘END {…}’ is executed, which prints the total number of fields.

$ awk -F ':' '{ total += NF }; END { print total }' /etc/passwd
49

Awk Example 2: Count number of users who is using /bin/sh shell

In the below awk script, it matches last field of all lines containing the pattern /bin/sh. Regular expression should be closed between //. So all the frontslash(/) has to be escaped in the regular expression. When a line matches variable ‘n’ gets incremented by one. Printed the value of the ‘n’ in the END section.

Awk Example 3: Find the user details who is having the highest USER ID

The below awk script, keeps track of the largest number in the field in variable ‘maxuid’ and the corresponding line will be stored in variable ‘maxline’. Once it has looped over all lines, it prints them out.

Awk Example 6: Print user details who has USER ID greater than or equal to 100 and who has to use /bin/sh

In the below Awk statement, there are two conditional expression one is User id($3) greater than or equal to 100, and second is last field should match with the /bin/sh , ‘&&’ is to print only if both the above conditions are true.

4 Awk If Statement Examples ( if, if else, if else if, :? )

In this awk tutorial, let us review awk conditional if statements with practical examples.

Awk supports lot of conditional statements to control the flow of the program. Most of the Awk conditional statement syntax are looks like ‘C’ programming language.

Normally conditional statement checks the condition, before performing any action. If the condition is true action(s) are performed. Similarly action can be performed if the condition is false.

Conditional statement starts with the keyword called ‘if’. Awk supports two different kind of if statement.

Awk Simple If statement

Awk If-Else statement

Awk If-ElseIf-Ladder

Awk Simple If Statement

Single Action: Simple If statement is used to check the conditions, if the condition returns true, it performs its corresponding action(s).

Syntax:
if (conditional-expression)
action

if is a keyword

conditional-expression – expression to check conditions

action – any awk statement to perform action.

Multiple Action: If the conditional expression returns true, then action will be performed. If more than one action needs to be performed, the actions should be enclosed in curly braces, separating them into a new line or semicolon as shown below.

Syntax:
if (conditional-expression)
{
action1;
action2;
}

If the condition is true, all the actions enclosed in braces will be performed in the given order. After all the actions are performed it continues to execute the next statements.

Awk If Else Statement

In the above simple awk If statement, there is no set of actions in case if the condition is false. In the awk If Else statement you can give the list of action to perform if the condition is false. If the condition returns true action1 will be performed, if the condition is false action 2 will be performed.

Syntax:
if (conditional-expression)
action1
else
action2

Awk also has conditional operator i.e ternary operator ( ?: ) whose feature is similar to the awk If Else Statement. If the conditional-expression is true, action1 will be performed and if the conditional-expression is false action2 will be performed.

Awk If Else If ladder

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.

The condition for Pass is all the test score mark should be greater than or equal to 35. So all the test scores are checked if greater than 35, then it prints the whole line and string “Pass”, else i.e even if any one of the test score doesn’t meet the condition, it prints the whole line and prints the string “Fail”.

3. Awk If Else If Example: Find the average and grade for every student

In the above awk script, the variable called ‘avg’ has the average of the three test scores. If the average is greater than or equal to 90, then grade is A, or if the average is greater than or equal to 80 then grade is B, if the average is greater than or equal to 70, then the grade is C. Or else the grade is D.

In this article, let us review about awk loopstatements – while, do while, for loops, break, continue, and exit statements along with 7 practical examples.
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.

1. Awk While Loop Example: Create a string of a specific length

The above example uses the ‘BEGIN { }’ special block that gets executed before anything else in an Awk program. In this block, awk while loop appends character ‘x’ to variable ‘string’ 50 times. count is a variable which gets incremented and checked it is less than 50. So after 50 iteration, this condition becomes false.
After it has looped, the ‘string’ variable gets printed out. As this Awk program does not have a body, it quits after executing the BEGIN block.

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)

Even if the condition is false, at the beginning action is performed at least once.

2. Awk Do While Loop Example: Print the message at least once

In the above script, the print statement, executed at least once, if you use the while statement, first the condition will be checked after the count is initialized to 1, at first iteration itself the condition will be false,so print statement won’t get executed, but in do while first body will be executed, so it executes print statement.

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.

3. Awk For Loop Example . Print the sum of fields in all lines.

Initially the variable i is initialized to 1, then checks if i is lesser or equal to total number of fields, then it keeps on adding all the fields and finally the addition is stored in the variable total. In the END block just print the variable total.

4. Awk For Loop Example: Print the fields in reverse order on every line.

In the above script, it prints value of x, at each iteration, but if the value of x reaches 5, then it just increment the value of x, then continue with the next iteration, it wont execute the rest body of the loop, so that value of x is not printed for the value 5. Continue statement is having the meaning only if you use with in the loop.

Awk Exit statement

Exit statement causes the script to immediately stop executing the current input and to stop processing input all the remaining input is ignored.

Exit accepts any integer as an argument which will be the exit status code for the awk process. If no argument is supplied, exit returns status zero.

About Me

Hi This is Suresh Kumar Pakalapati . I am a person who is positive about every aspect of life. There are many things I like to do, to see, and to experience. I like to feel the music flowing on my face, I like good books and romantic, action,cartoon movies. I like the land and the nature, And, I like to keep smile on my face. I always wanted to be a great and successful person in the world and I know it is needed More and More education, More work, Success Never come in a short Time.....