The awk language
Download
1 / 12

The AWK Language - PowerPoint PPT Presentation


  • 64 Views
  • Uploaded on

제 14 강 : The AWK Language. The AWK Language. While reading each line from file Pattern matching , Action. File sample Koh 4.00 0 Lee 3.75 0 Park 4.00 10 Kim 5.00 20 Song 5.50 22 Han 4.25 18. pattern. action. awk ‘$3>0 {print $1, $2, $3}’ sample. awk program. Example

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'The AWK Language' - lydie


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
The awk language

14강 : The AWK Language

The AWK Language


While reading each line from file

Pattern matching, Action

File sample

Koh 4.00 0

Lee 3.75 0

Park 4.00 10

Kim 5.00 20

Song 5.50 22

Han 4.25 18

pattern

action

  • awk ‘$3>0 {print $1, $2, $3}’ sample

awk program

  • Example

    • awk ‘$3==0 {print $1}’ sample

    • awk ‘$3>0 {print $1, $2*$3}’ sample

    • 3) awk ‘{print $1}’ sample : action with no pattern

Output 2

Park 40

Kim 100

Song …

Han …

Output 1

Koh

Lee


Structure of

an awk program:

Syntax

awk ‘program’ input_files

awk –f program_file input_files

pattern {action}

pattern {action}

  • Simple output

    • {print} entire line

    • {print $0} entire line

    • {print NF $1} NF : built-in var

    • {print $1, $NF} print 1st & last field

    • {print NR $0} line_# & line

    • {print “total pay for”, $1, “is”, $2*$3}

Variables ?

See next slide …


Variables

User-defined : 0 or null init

Built-in

Fields : $0 $1 $2 …

Printf format conversion

% c : ASCII char

% d : decimal number

% o : unsigned octal

% f, % e, % s, ….

Example

{printf(“pay for % s is w% .2f \n”, $1, $2*$3)}

Built-in vars

ARGC : # of command line args

ARGV : array of command line args

FILENAME : name of input file

FNR : record # in current file

FS : field separator

NF : # of fields in current rec

NR: # of recs read so far

OFMT : output format for numbers

OFS : output field separator

RS : input rec separator

File sample

Koh 4.00 0

Lee 3.75 0

Park 4.00 10

Kim 5.00 20

Song 5.50 22

Han 4.25 18

output

Pay for Han is w76.50


File sample

Koh 4.00 0

Lee 3.75 0

Park 4.00 10

Kim 5.00 20

Song 5.50 22

Han 4.25 18

  • Selection

    • By computation

      • awk ‘$2*$3>50 {printf(“$%.2f for %s\n”, $2*$3, $1)}’ sample

    • By text content

      • awk ‘$1==“Susie” {print …}’ sample

    • combinations (&& || !)

      • awk ‘$2 >= 4 || $3 >= 30 {printf …}’ sample

Output

$100.00 for Kim

$121.00 for Song

$76.50 for Han


File sample

Koh 4.00 0

Lee 3.75 0

Park 4.00 10

Kim 5.00 20

Song 5.50 22

Han

  • Pattern BEGIN & END

    • BEGIN { statements }

      • Becomes TRUE once

        before any input has been read

    • END { statements }

      • Becomes TRUE once

        after all input has been read

    • example

try sf11

BEGIN {print “Header line here”}

$3>15 {emp=emp+1}

END {print emp, “men worked more than 15 hours”}

emp : user- defined var

initialized value zero

user var can hold strings as well as numbers

$2>maxrate {maxrate=$2, maxemp=$1}

END {print “max. rate:”, maxrate, “for”, maxemp}


names var =

Koh Lee Park Kim

separated by blanks

{names=names $1 ““}

END {print names}

{print $1, length($1)}

  • String concatenation

  • Built-in string functions

    • match(s, r) : the leftmost longest substr in s

      that is matched by the regular expr r

    • split(s, a, fs) : splits the str s into the array a

      according to the separator fs

      and returns # of elements

    • sub(r, s, t) : find leftmost longest substr

    • gsub(r, s, t) : global substitution


String examples
string examples

  • $4 ~/Asia/ {Action}

    • 4th field contains Asia

  • $0 !~/Asia/ {Action}

    • line doesn’t contain Asia

  • $2 !~/^[0-9]+$/ {Action}

    • 2nd field not a string of digits


Built in arithmetic functions
Built-in arithmetic functions

  • atan2(y, x) : arctangent of y/x

  • cos(x) : cosine of x

  • exp(x) : ex

  • int(x) : integer part of x

  • log(x) : natural logarithm of x

  • rand() : random number

  • sin(x) : sine of x

  • sqrt(x) : square root of x

  • srand(x) : x is new seed for rand()


  • Control statements

    • if (expr) statement

    • if (expr) statementelsestatement

    • while (expr) statement

    • for (expr;expr;expr) statement

    • dostatementwhile (expr)

    • break

    • continue

  • Arrays

{ line[NR] = $0 }

END { i=NR

While (i>0) {…}

}


Range patterns
Range patterns

  • pattern1, pattern2 { Action }

    • A range pattern matches each input line

    • from a line matched by pattern1

    • to the next line matched by pattern2

  • Example: Removing unwanted part

    #ifdef intel  pattern1

    intel dependent code “remove”

    #elseif  pattern2

    non-intel code

    #endif


Awk is
Awk is

  • a UNIX command

  • Interpreter

    • Given a file,

    • Read line by line

    • Pattern match & take action

  • Text processing tool

    • More powerful than grep, sed, …

  • Filter

    • Given a text file

    • Read each line

    • Transforms them

    • Output lines


ad