100% found this document useful (1 vote)
65 views25 pages

Lokesh (3054) Kushal (3053)

AWK is a programming language designed for processing text-based data. It was created at Bell Labs in the 1970s by Alfred Aho, Peter Weinberger, and Brian Kernighan. AWK is useful for tasks like tallying information from text files, adding functions to text editors, translating file formats, and performing mathematical operations on numeric data files. An AWK program consists of pattern-action statements that are applied to each input line.

Uploaded by

saxenaamit_mca
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
65 views25 pages

Lokesh (3054) Kushal (3053)

AWK is a programming language designed for processing text-based data. It was created at Bell Labs in the 1970s by Alfred Aho, Peter Weinberger, and Brian Kernighan. AWK is useful for tasks like tallying information from text files, adding functions to text editors, translating file formats, and performing mathematical operations on numeric data files. An AWK program consists of pattern-action statements that are applied to each input line.

Uploaded by

saxenaamit_mca
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

AWK

Lokesh (3054)
Kushal (3053)
WHAT IS AWK!!!

AWK is a programming language that is


designed for processing text-based data.
INTRODUCTION
created at Bell Labs in the 1970s

The name AWK is derived from the family names of


its authors — Alfred Aho, Peter Weinberger, and
Brian Kernighan.

AWK is one of the early tools to appear in 7th


Version of Unix and gained popularity as a way to add
computational features to a Unix pipeline.
TASKS
Tallying information from text files and creating
reports from the results.
Adding additional functions to text editors like "vi".
Translating files from one format to another.
Creating small databases.
Performing mathematical operations on files of
numeric data.
PARADIGMS
Scripting language - Besides the Bourne shell, AWK
is the only other scripting language available in a
standard Unix environment.

 Procedural language - AWK is an example of a


programming language that extensively uses the
string datatype, associative arrays

 Event driven - AWK reads the input a line at a time. A


line is scanned for each pattern in the program, and for
each pattern that matches, the associated action is
executed.
Structure of an AWK Program
An Awk program consists of:
An optional BEGIN segment BEGIN
 For processing to execute prior to pattern {action}
reading input pattern {action}
pattern - action pairs .
 Processing for input data .
 For each pattern matched, the
.
corresponding action is taken pattern { action}
An optional END segment
END
 Processing after end of input data

6
HANDLING TEXT
One major advantage of Awk is its ability to
handle strings as easily as many languages
handle numbers

Awk variables can hold strings of characters


as well as numbers, and Awk conveniently
translates back and forth as needed
BEGIN and END
Special pattern BEGIN matches before the first input
line is read; END matches after the last input line has
been read
This allows for initial and wrap-up processing
BEGIN { print “NAME RATE HOURS”; print “” }
{ print }
END { print “total number of employees is”, NR }
Hello world application


BEGIN { print " Hello, world!" }
Some of the Built-In Variables
NF - Number of fields in current record
NR - Number of records read so far
$0 - Entire line
$n - Field n
$NF - Last field of current record
LIKE C LANGUAGE
IT HAS…..
OPERATORS
Operators in Increasing Precedence

 Assignment:= , +=, -=, *=, /=, %=, ^=


 Logical: ||, &&, ~, !~
 Relational: <, <=, ==, !=, >=, >

 Concatenation: blank
 Add/Subtract: +, -
 Multiply/divide/mod: *, /, %
 Unary plus, minus, not, exponent (^ or **)
 Increment, decrement

 Field: $expr
String Concatenation


New strings can be created by combining old
ones
{ names = names $1 “ “ }
END { print names }
CONTINUE……
Arrays
 Associative arrays (hash): index can be any value (integer or string)
 Referencing creates entry:

if (arr[“x”] != “”) print “arr[x] exists”


Control flow statements
 if ( expr ) statement [ else statement ]
 if ( subscript in array ) ...

 while ( expr ) statement

 for ( init_expr; test_expr; increment_expr ) statement

 for ( subscript in array ) statement

 do statement while ( expr )

 break, continue, next,exit , return [expr]


Command Line Arguments
Accessed via built-ins ARGC and ARGV
ARGC is set to the number of command line
arguments
ARGV[ ] contains each of the arguments
For the command line
awk filename
 ARGC == 2
 ARGV[0] == “awk”
 ARGV[1] == “filename
ARGC/ARGV in Action
#argv.awk – get a cmd line argument and display
BEGIN {if(ARGC != 2)
{print "Not enough arguments!"}
else
{print "Good evening,", ARGV[1]}
}
getline
How do you get input into your awk script other than
on the command line?
The getline function provides input capabilities
getline is used to read input from either the current
input or from a file or pipe
getline returns 1 if a record was present, 0 if an end-of-
file was encountered, and –1 if some error occurred
Getline -$0, NF, NR,
getline from stdin
#getline.awk - demonstrate the getline function
BEGIN {print "What is your first name and major? "
while (getline > 0)
print "Hi", $1 ", your major is", $2 "."
}
Control Flow Statements
Awk provides several control flow statements for
making decisions and writing loops
If-Else
$2 > 6 { n = n + 1; pay = pay + $2 * $3 }
END { if (n > 0)
print n, “employees, total pay is”, pay,
“average pay is”, pay/n
else
print “no employees are paid more than $6/hour”
}
Loop Control
While
# interest- compute compound interest
# input: amount rate years
# output: compound value at end of each year
{ i=1
while (i <= $3) {
printf(“\t%.2f\n”, $1 * (1 + $2) ^ i)
i=i+1
}
}
Loop Control
{ for (i = NF; i > 0; i = i - 1) printf(“%s “, $i)
printf(“/n”)
}
{ sum = 0
for (i = 1; i <= NF; i = i + 1) sum = sum + $i
print sum
{
 { for (i = 1; i <= NF; i = i + 1) sum = sum $i }
END { print sum }
Built-In Functions
Arithmetic
sin, cos, atan, exp, int, log, rand, sqrt
String
length, substitution, find substrings, split strings
Output
print, printf, print and printf to file
Special
system - executes a Unix command
 system(“clear”) to clear the screen
 Note double quotes around the Unix command

exit - stop reading input and go immediately to the END


pattern-action pair if it exists, otherwise exit the script
Formatted Output
printf provides formatted output
Syntax is printf(“format string”, var1, var2, ….)
Format specifiers
 %c – single character
 %d - number
 %f - floating point number
 %s - string
 \n - NEWLINE
 \t - TAB
Example:Transpose of matrix
BEGIN {count = 1;
for (row = 1; row <= 5; ++row) {
for (col = 1; col <= 3; ++col) {
printf("%4d",count);
array[row,col] = count++; }
printf("\n"); }
printf("\n");
for (col = 1; col <= 3; ++col)
{ for (row = 1; row <= 5; ++row) { printf("%4d",array[row,col]); }
printf("\n"); }
exit; }
Thank you

You might also like