52048386-what-is-awk

Upload: santoshpanditpur

Post on 04-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 52048386-WHAT-IS-AWK

    1/25

    AWK

    Lokesh (3054)

    Kushal (3053)

  • 7/31/2019 52048386-WHAT-IS-AWK

    2/25

    AWK is a programming language that is

    designed for processing text-based data.

  • 7/31/2019 52048386-WHAT-IS-AWK

    3/25

    INTRODUCTIONy created at Bell Labs in the 1970s

    y The name AWK is derived from the familynames ofitsauthors Alfred Aho, Peter Weinberger, and BrianKernighan.

    yAWK is one of the early tools to appear in 7thVersionof Unix and gained popularity as a way to addcomputational features to a Unix pipeline.

  • 7/31/2019 52048386-WHAT-IS-AWK

    4/25

    TASKSy Tallying information from text files and creating

    reports from the results.

    yAdding additional functions to text editors like "vi".y Translating files from one format to another.

    y Creating small databases.

    y Performing mathematical operations on files of

    numeric data.

  • 7/31/2019 52048386-WHAT-IS-AWK

    5/25

    PARADIGMS

    y Scripti g language - esides t e urne s ell,is t e nly t er scripting language

    availa le in a standard nix envir n ent.

    y r cedural language - is an exa ple f apr gra ing language t at extensivelyuses

    t e string datatype, ass ciative arrays

    yEvent driven - reads t e input a line at atime. line is scanned f r eac pattern in t e

    pr gram, and f r eac pattern t at matc es, t eass ciated action is executed.

  • 7/31/2019 52048386-WHAT-IS-AWK

    6/25

    6

    B GIN

    pattern {action}

    pattern {action}

    .

    .

    .

    pattern { action}

    END

    Structure of an AWK Program

    yAn Awk program consists of:

    y An optional BEGIN segment

    y

    For processing to execute prior toreading input

    y pattern - action pairs

    y Processing for input data

    y For each pattern matched, the

    corresponding actionis takeny An optional END segment

    y Processing after end ofinput data

  • 7/31/2019 52048386-WHAT-IS-AWK

    7/25

    HANDLING TEXTyOne major advantage of Awk is its ability to

    handle strings as easily as many languageshandle numbers

    yAwkvariables can hold strings of charactersas well as numbers, and Awk convenientlytranslates back and forth as needed

  • 7/31/2019 52048386-WHAT-IS-AWK

    8/25

    BEGIN and END

    y Special pattern BEGIN matches before the first inputline is read; END matches after the last input line has

    been ready This allows for initial and wrap-up processing

    BEGIN { print NAME RATE HOURS; print }

    { print }

    END { print total number of employees is, NR}

  • 7/31/2019 52048386-WHAT-IS-AWK

    9/25

    Hello world application

    y BEGIN { print "Hello, world!" }

  • 7/31/2019 52048386-WHAT-IS-AWK

    10/25

    Some of the Built-In Variables

    y NF - Number of fields in current record

    y NR - Number of records read so fary $0 - Entire line

    y $n - Field n

    y

    $NF - Last field of current record

  • 7/31/2019 52048386-WHAT-IS-AWK

    11/25

    yLIKE C LANGUAGE

    IT HAS..

  • 7/31/2019 52048386-WHAT-IS-AWK

    12/25

    OPERATORSy Operators in Increasing Precedence

    y

    Assignment: = , +=, -=, *=, /=, %=, =y Logical: ||, &&, ~, !~

    y Relational:

    y Concatenation: blank

    y Add/Subtract: +, -

    y Multiply/divide/mod: *, /, %

    y Unary plus, minus, not, exponent (^ or **)

    y Increment, decrement

    y Field: $expr

  • 7/31/2019 52048386-WHAT-IS-AWK

    13/25

    Stri g cat ati

    y New strings can be created by combining oldones

    { names = names $1 }

    END { print names }

  • 7/31/2019 52048386-WHAT-IS-AWK

    14/25

    CONTINUEy Arrays

    y Associative arrays (hash): index can be anyvalue (integer or string)

    y Referencing creates entry:

    if (arr[x] != ) print arr[x] existsy Control flow statements

    y if ( expr) statement [ else statement ]

    y if ( subscript inarray ) ...

    y while ( expr) statement

    y for ( init_expr; test_expr; increment_expr) statementy for ( subscript inarray ) statement

    y do statementwhile ( expr)

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

  • 7/31/2019 52048386-WHAT-IS-AWK

    15/25

    Comma Li rg mentsyAccessedvia built-ins ARGC and ARGV

    yARGC is set to the number of command line

    argumentsyARGV[ ] contains each of the arguments

    y For the command line

    y awk f ilename

    y ARGC == 2y ARGV[0] == awk

    y ARGV[1] == filename

  • 7/31/2019 52048386-WHAT-IS-AWK

    16/25

    ARGC/ARGV inAction#argv.awk get a cmd line argument and display

    BEGIN {if(ARGC != 2)

    {print "Not enough arguments!"}else

    {print "Good evening,", ARGV[1]}

    }

  • 7/31/2019 52048386-WHAT-IS-AWK

    17/25

    getliney How do you get input into your awk script other than

    on the command line?

    y

    Thegetline function provides input capabilitiesygetline is used to read input from either the current

    input or from a file or pipe

    ygetline returns 1 if a record was present, 0 if an end-of-

    file was encountered, and 1 if some error occurredy Getline -$0, NF, NR,

  • 7/31/2019 52048386-WHAT-IS-AWK

    18/25

    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 "."

    }

  • 7/31/2019 52048386-WHAT-IS-AWK

    19/25

    Control Flow StatementsyAwk provides several control flow statements for

    making decisions and writing loops

    y If-Else$2 > 6 { n = n + 1; pay = pay + $2 * $3 }

    END { if (n > 0)

    print n, employees, total payis, pay,

    average payis, pay/n

    else

    print no employees are paid more than $6/hour

    }

  • 7/31/2019 52048386-WHAT-IS-AWK

    20/25

    Loop ControlyWhile

    #interest- compute compound interest

    #

    input: amount rate years# output: compoundvalue at end of each year

    { i = 1

    while (i

  • 7/31/2019 52048386-WHAT-IS-AWK

    21/25

    Loop Controly { for (i = NF; i > 0; i = i - 1) printf(%s , $i)

    printf(/n)

    }y { sum = 0

    for (i = 1; i

  • 7/31/2019 52048386-WHAT-IS-AWK

    22/25

    Built-In FunctionsyArithmetic

    y sin, cos, atan, exp, int, log, rand, sqrt

    y Stringy length, substitution, find substrings, split strings

    y Outputy print, printf, print andprintfto file

    y Special

    y system - executes a Unix commandy system(clear) to clear the screeny Note double quotes around the Unix command

    y exit - stop reading input and go immediately to the ENDpattern-action pair ifit exists, otherwise exit the script

  • 7/31/2019 52048386-WHAT-IS-AWK

    23/25

    Formatted Outputy printfprovides formatted output

    y Syntax isprintf(format string, var1, var2, .)

    y

    Format specifiersy %c single character

    y %d - number

    y %f - floating point number

    y %s - string

    y \n - NEWLINE

    y \t - TAB

  • 7/31/2019 52048386-WHAT-IS-AWK

    24/25

    Example:Transpose of matrixy BEGIN {count = 1;

    for (row = 1; row

  • 7/31/2019 52048386-WHAT-IS-AWK

    25/25

    y Thank you