P. 1
52048386-WHAT-IS-AWK

52048386-WHAT-IS-AWK

|Views: 1|Likes:
Published by santosh_panditpur

More info:

Published by: santosh_panditpur on Oct 18, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/18/2012

pdf

text

original

AWK

Lokesh (3054) Kushal (3053)

AWK is a programming language that is designed for processing text-based data.

y 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. .INTRODUCTION y created at Bell Labs in the 1970s y The name AWK is derived from the family names of its authors Alfred Aho. and Brian Kernighan. Peter Weinberger.

TASKS y Tallying information from text files and creating y y y y reports from the results. Translating files from one format to another. Creating small databases. Performing mathematical operations on files of numeric data. . Adding additional functions to text editors like "vi".

t e ass ciated action is executed. ass ciative arrays reads t e input a line at a time. is t e nly t er scripting language availa le in a standard nix envir n ent. y Event driven - .esides t e urne s ell. line is scanned f r eac pattern in t e pr gram.PARADIGMS y Scripti g language . and f r eac pattern t at matc es. y r cedural language - is an exa ple f a pr gra ing language t at extensively uses t e string datatype.

. pattern { action} END For processing to execute prior to reading input Processing for input data For each pattern matched.action pairs y y y An optional END segment y 6 .Structure of an AWK Program y An Awk program consists of: y An optional BEGIN segment y B GIN pattern {action} pattern {action} . . the corresponding action is taken Processing after end of input data y pattern .

HANDLING TEXT y One major advantage of Awk is its ability to handle strings as easily as many languages handle numbers y Awk variables can hold strings of characters as well as numbers. and Awk conveniently translates back and forth as needed .

BEGIN and END y Special pattern BEGIN matches before the first input line is read. NR } . print } { print } END { print total number of employees is . END matches after the last input line has been read y This allows for initial and wrap-up processing BEGIN { print NAME RATE HOURS .

world!" } .Hello world application y BEGIN { print " Hello.

Entire line y $n .Last field of current record .Number of fields in current record y NR .Field n y $NF .Number of records read so far y $0 .Some of the Built-In Variables y NF .

.yLIKE C LANGUAGE IT HAS . .

*=. >=. ^= Logical: ||. !~ Relational: <. /. %=. &&. minus. -=. exponent (^ or **) Increment. > Concatenation: blank Add/Subtract: +. /=. +=.OPERATORS y Operators in Increasing Precedence y y y y y y y y y Assignment: = . Multiply/divide/mod: *. not. ==. decrement Field: $expr . ~. !=. % Unary plus. <=.

Stri g cat ati y New strings can be created by combining old } ones { names = names $1 END { print names } .

while ( expr ) statement for ( init_expr.exit . increment_expr ) statement for ( subscript in array ) statement do statement while ( expr ) break. next.CONTINUE y Arrays y y Associative arrays (hash): index can be any value (integer or string) Referencing creates entry: if (arr[ x ] != ) print arr[x] exists if ( expr ) statement [ else statement ] if ( subscript in array ) .. test_expr. continue.. return [expr] y Control flow statements y y y y y y y .

Comma Li rg ments y Accessed via built-ins ARGC and ARGV y ARGC is set to the number of command line arguments y ARGV[ ] contains each of the arguments y For the command line y awk filename y y y ARGC == 2 ARGV[0] == awk ARGV[1] == filename .

ARGV[1]} } .".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.

. and 1 if some error occurred Getline -$0.getline y How do you get input into your awk script other than y y y y 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-offile was encountered. NF. NR.

awk ." } .demonstrate the getline function BEGIN {print "What is your first name and major? " while (getline > 0) print "Hi". your major is".getline from stdin #getline. $2 ". $1 ".

total pay is . pay. employees. average pay is . pay = pay + $2 * $3 } END { if (n > 0) print n. pay/n else print no employees are paid more than $6/hour } .Control Flow Statements y Awk provides several control flow statements for making decisions and writing loops y If-Else $2 > 6 { n = n + 1.

$1 * (1 + $2) ^ i) i=i+1 } } .compute compound interest # input: amount rate years # output: compound value at end of each year { i=1 while (i <= $3) { printf( \t%.2f\n .Loop Control y While # interest.

i = i + 1) sum = sum + $i print sum { y { for (i = 1. i = i + 1) sum = sum $i } END { print sum } . i > 0. i <= NF.Loop Control y { for (i = NF. i = i . i <= NF. $i) printf( /n ) } y { sum = 0 for (i = 1.1) printf( %s .

atan. otherwise exit the script . split strings y Output y print. sqrt y String y length. log. print and printf to file y Special y system . substitution. printf.Built-In Functions y Arithmetic y sin. rand.stop reading input and go immediately to the END pattern-action pair if it exists. cos. exp.executes a Unix command y y system( clear ) to clear the screen Note double quotes around the Unix command y exit . find substrings. int.

string \n .NEWLINE \t .number %f . var2.TAB .) y Format specifiers y y y y y y %c single character %d .Formatted Output y printf provides formatted output y Syntax is printf( format string . var1.floating point number %s . .

} printf("\n"). for (row = 1. ++col) { printf("%4d".array[row. ++row) { printf("%4d". } exit. } . ++col) { for (row = 1. col <= 3.Example:Transpose of matrix y BEGIN {count = 1. } printf("\n"). } printf("\n"). row <= 5. row <= 5. ++row) { for (col = 1.col] = count++.col]). array[row. for (col = 1. col <= 3.count).

y Thank you .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->