Professional Documents
Culture Documents
About AWK
1) AWK is a report formatting tool 2) Named after Aho, Weinberger and Kernighan 3) AWK operates at the field level 4) AWK can easily access, transform and format individual fields in a line. 5) AWK has C programming language type constructs, variables and several built-in functions.
Here, selection_criteria section (/sales/) selects lines that are processed in the action section ({print})
Remember
Either of the selection_criteria and action is optional, but not both and they must be enclosed within a pair of single quotes. The print statement when used without any field specifiers, prints the entire line. Since printing is the default action of the awk, it can be omitted.
awk '/director/' employee.lst awk '/director/ { print}' employee.lst awk '/director/ {print $0 }' employee.lst
Output
TRUE
An awk program must have either the selection criteria or the action or both, but within single quotes.
True
Remember Since $1, $2, $3 etc have got special meaning to the shell, we use single-quotes to protect the interpretation by the shell. Awk uses whitespace as the default delimeter
Write a simple awk to print name, designation, department and alary of all the admin dept. people.
awk has a built-in variable, NR, to specify line number. Write a awk script to select lines 3 to 6 and print line number, name, designation and salary.
The Comparison Operators Write an awk script to print the name, designation and salary of all directors and chairman.
For negating the above condition, we use != Write an awk script to print the name, designation and salary of all employees who are neither directors nor chairman.
Write an awk script to print the name, designation and salary of all employees who are neither directors nor chairman.
True
Number Comparison Write an awk to print name, designation and salary of all those people whose pay exceeds 6000.
False
Number Processing
Write an awk script to print salary slip of all directors, assume that the 6th field gives basic salary, compute DA @ 40% of basic pay, HRA @ 15% of basic pay.
Variables
1) No type declarations needed 2) By default initialized to zero or null string Write an awk script to print serial numbers for all those directors drawing salary exceeding 6500.
Check your understanding True / False In awk, for user defined variables no type declarations or initial values are required.
The -f option. Storing awk programs in a file AWK programs can be stored in a file with .awk extension.
Note
awk uses single quotes only when the program is specified in the command line or when the entire awk command line is held in a shell script.
The BEGIN and END sections awk provides BEGIN section to allow us to have a header before printing something and END section to allow us to have a footer, print something after the processing is over.
Write an awk script to print a suitable header at the beginning and average salary at the end of all employees with line numbers.
Write an awk script to locate those lines not having 3 fields from the file empx.lst
Write an awk script, to print the total of the basic pay, da , hra and gross of sales and marketing people [use arrays]
Functions
awk has several built-in functions for performing both arithmetic and string operations.
int(x) - Usage
sqrt(x) - usage
length - usage
length(x) - usage
index(s1,s2) - usage
substr(stg,m,n) -usage
Write an awk script using substr function, to select those born between 1946 and 1951:
split(stg,arr,ch) - usage
split(stg,arr,ch) breaks up a string stg on the delimeter ch and stores the fields in an arrar arr[]. Write an awk to covert the date field from the current DD/MM/YY to the format YYYY-MM-DD
system(cmd) - usage
if - usage
Write an awk to print the director pay slip, assume that 6th field signifies basic salary; if the basic pay is less than 6000, compute DA=25% of basic and 1000 otherwise.
awk 'BEGIN {FS="|"; system("clear"); printf "\n\t Director Pay Slip\n";} /director/ { if ($6 > 6000) {DA=$6*0.25} else DA=1000; printf "%5s %d %d\n",$2,$6,DA}' employee.lst
awk 'BEGIN {FS="|"; system("clear"); printf "\n\t Director Pay Slip\n";} /director/ {$6 > 6000 ? DA=$6*0.25 : DA=1000; printf "%5s %d %d\n",$2,$6,DA}' employee.lst
for(k=1;k<=9;k++)
Write an awk to center the text given to awk by echo using for loop.
{
commands }
Write an awk to display a count of employees, grouped according to designation(3rd field). Hint : [$3 can be used as a subscript of an array]
Note
We had implemented the same logic using tr, cut,sort and uniq commands.
Thank YOU