awk programming

awk Programming

Awk :-Created by: Aho, Weinberger, and Kernighan. It is a scripting language used for manipulating data and generating reports

versions of awk :-
1) awk, nawk, mawk, pgawk, …
2) GNU awk: gawk

awk operation:-
    1) scans a file line by line
    2) splits each input line into fields
    3) compares input line/fields to pattern
    4) performs action(s) on matched lines

Useful for
:- awk is useful for beow
    1) Transform data files
    2) produce formatted reports
    3) Programming constructs
    4) Format output lines
    5) Arithmetic and string operations
    6) Conditionals and loops

Syntax :-
awk -F; 'pattern {action}' file(s) --Here "-F;" is denote that input file has ";" as a field separator
awk –f scriptfile file(s)

Options:
 -F  For input field separator
 -f  For script file
   
Note:- if pattern is missing, action is applied to all lines, if action is missing, the matched line is printed and must have either pattern or action

Example:-prints all lines containing string "for" in testfile
    awk '/for/' testfile
   
BASIC TERMINOLOGY:-

A field is a unit of data in a line
Each field is separated from the other fields by the field separator
Default field separator is whitespace
A record is the collection of fields in a line
A data file is made up of records

BUFFERS:- awk supports two types of buffers: 1) record buffer 2) field buffer

Field buffer:-
    one for each fields in the current record.
    names: $1, $2, …

Record buffer:-
      $0 holds the entire record

SYSTEM VARIABLES:-
FS:-  Field separator (default=whitespace)
RS:-  Record separator (default=\n)
NF:-  Number of fields in current record
NR:-  Number of the current record
OFS:- Output field separator (default=space)
ORS:- Output record separator (default=\n)

Examples:-
%cat emps
Tom Jones        4424    5/12/66    543354
Mary Adams       5346    11/4/63    28765
Sally Chang      1654    7/22/54    650000
Billy Black      1683    9/23/44    336500

%awk '{print NR, $0}' emps
1   Tom Jones     4424    5/12/66   543354
2   Mary Adams    5346    11/4/63   28765
3   Sally Chang   1654    7/22/54   650000
4   Billy Black   1683    9/23/44   336500  


%awk '{print NR, $1, $2, $5}' emps
1   Tom Jones 543354
2   Mary Adams 28765
3   Sally Chang 650000
4   Billy Black 336500                     

%cat em2
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500

% awk -F: '/Jones/{print $1, $2}' em2  --Will print the lines which have "Jones" in it's contents
Tom Jones 4424
                   
AWK FULL SYNTAX :-
BEGIN {statement;statement;statement}  --Optional
       pattern {statement;statement;statement}  --Mandatory
END   {statement;statement;statement}  --Optional
              OR
BEGIN {statement
       statement
       statement}
      pattern {statement
       statement
       statement}
END   {statement
       statement
       statement}
Note:- If we want multiple statement in same line then need to use ";" for statement separator.
              

awk scripts are divided into three major parts:
comment lines start with #                       

BEGIN: pre-processing
    Performs processing that must be completed before the file processing starts 

    (i.e., before awk  starts reading records from the input file)
    Useful for initialization tasks such as to initialize variables and to create report headings
BODY: Processing
    Contains main processing logic to be applied to input records
    Like a loop that processes input data one record at atime:
    If a file contains 100 records, the body will be executed 100 times, one for each record
END: post-processing
    Contains logic to be executed after all input data have been processed
    Logic such as printing report grand total should be performed in this part of the script


CATEGORIES OF PATTERNS:-
EXPRESSION PATTERN TYPES

match
    Entire input record regular expression enclosed by //
    Explicit pattern-matching expressions
      ~ (match), !~ (not match)

expression operators
    Arithmetic
    Relational
    Logical

Examples:-
% cat employees2
Tom Jones:4424:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500

% awk –F: '/00$/' employees2  --Will print all lines with ending with 00 and : will treated as a field separator
Sally Chang:1654:7/22/54:650000
Billy Black:1683:9/23/44:336500

% cat datafile
northwest  NW    Charles Main        3.0   .98   3   34
western    WE    Sharon Gray         5.3   .97   5   23
southwest  SW    Lewis Dalsass       2.7   .8    2   18
southern   SO    Suan Chin           5.1   .95   4   15
southeast  SE    Patricia Hemenway   4.0   .7    4   17
eastern    EA    TB Savage           4.4   .84   5   20
northeast  NE    AM Main             5.1   .94   3   13
north      NO    Margot Weber        4.5   .89   5    9
central    CT    Ann Stephens        5.7   .94   5   13

% awk '$5 ~ /\.[7-9]+/' datafile
southwest SW     Lewis Dalsass       2.7   .8    2   18
central    CT    Ann Stephens        5.7   .94   5   13  

% awk '$2 !~ /E/{print $1, $2}' datafile
northwest NW
southwest SW
southern SO
north NO
central CT

% awk '/^[ns]/{print $1}' datafile
northwest
southwest
southern
southeast
northeast                                 
north

Arithmetic Operator:-
Operator   Meaning       Example
 +         Add           x+y
 -         Subtract      x–y
 *         Multiply      x*y
 /         Divide        x/y
 %         Modulus       x%y
 ^         Exponential   x^y

Example:
% awk '$3 * $4 > 500 {print $0}' file
                                        24
Relational Operator:-
Operator   Meaning                    Example
 <         Less than                  x <=        Less than or equal         x<=y
 ==        Equal to                   x == y
 !=        Not equal to               x != y
 >         Greater than               x>y
 >=        Greater than or equal to   x>=y
 ~         Matched by reg exp         x ~ /y/
 !~        Not matched by req exp     x !~ /y/
                                                 25
Logical Operator:-
Operator       Meaning        Example
 &&            Logical AND    a && b
 ||                 Logical OR       a || b
 !                 NOT                  !a

Examples:
% awk '($2 > 5) && ($2 <= 15)
                     {print $0}' file
% awk '$3 == 100 || $4 > 50' file

Range Pattern:-   Matches ranges of consecutive input lines
Syntax:- pattern1 , pattern2 {action}

pattern can be any simple pattern
pattern1 turns action on
pattern2 turns action off

RANGE PATTERN EXAMPLE
 

Expressions:-
Expression is evaluated and returns value
        Consists of any combination of numeric and string
        Constants, variables, operators, functions, and regular expressions can involve variables
       As part of expression evaluation
       As target of assignment

Variables :-

A user can define any number of variables within an awk script
The variables can be numbers, strings, or arrays
Variable names start with a letter, followed by letters, digits, and underscore
Variables come into existence the first time they are referenced; therefore, they do not need to be
  declared before use
All variables are initially created as strings and initialized to a null string “”

Syntax:- variable = expression

Examples:
% awk '$1 ~ /Tom/ {wage = $3 * $4; print wage}' filename
% awk '$4 == "CA" {$4 = "California"; print $0}' filename

Assignment Operator:-
=    assign result of right-hand-side expression to left-hand-side variable
++     Add 1 to variable
--       Subtract 1 from variable
+=      Assign result of addition
-=      Assign result of subtraction
*=      Assign result of multiplication
/=       Assign result of division
%=     Assign result of modulo
^=       Assign result of exponentiation

Examples:-

File: grades
  john 85 92 78 94 88
  andrea 89 90 75 90 86
  jasper 84 88 80 92 84

awk script: average
  # average five grades
  { total = $2 + $3 + $4 + $5 + $6
     avg = total / 5
     print $1, avg }

Run as:
  awk –f average grades             

OUTPUT STATEMENTS
print :- print easy and simple output
printf :- print formatted (similar to C printf)
sprintf :-format string (similar to C sprintf)
 

PRINT :-
Writes to standard output
Output is terminated by ORS
Default ORS is newline
If called with no parameter, it will print $0
Printed parameters are separated by OFS,
Default OFS is blank
Print control characters are allowed:- \n \f \a \t \\ …

Examples:-
% awk '{print}' grades
john 85 92 78 94 88
andrea 89 90 75 90 86

% awk '{print $0}' grades
john 85 92 78 94 88
andrea 89 90 75 90 86

% awk '{print($0)}' grades
john 85 92 78 94 88
andrea 89 90 75 90 86

% awk '{print $1, $2}' grades
john 85
andrea 89

% awk '{print $1 "," $2}' grades
john,85
andrea,89

% awk '{OFS="-";print $1 , $2}' grades
john-85
andrea-89

% awk '{OFS="-";print $1 "," $2}' grades
john,85
andrea,89

REDIRECTING PRINT OUTPUT:-
Print output goes to standard output unless redirected via:
    > “file”
    >> “file”
    | “command”
will open file or command only once
subsequent redirections append to already open stream

Examples:- 
% awk '{print $1 , $2 > "file"}' grades
% cat file
john 85
andrea 89
jasper 84

% awk '{print $1,$2 | "sort"}' grades
andrea 89
jasper 84
john 85

% awk '{print $1,$2 | "sort –k 2"}' grades
jasper 84
john 85
andrea 89

% date
Wed Nov 19 14:40:07 CST 2008

% date | awk '{print "Month: " $2 "\nYear: ", $6}'
Month: Nov
Year: 2008

PRINTF FORMATTING OUTPUT:-
Syntax:- printf(format-string, var1, var2, …)

  works like C printf
  each format specifier in “format-string” requires argument of matching type

FORMAT SPECIFIERS
%d, %i   decimal integer
%c       single character
%s       string of characters
%f       floating point number
%o       octal number
%x       hexadecimal number
%e       scientific floating point notation
%%       the letter “%”

FORMAT SPECIFIER EXAMPLES
Given: x = ‘A’, y = 15, z = 2.3, and $1 = Bob Smith
Printf Format
Specifier         What it Does
       %c         printf("The character is %c \n", x)
                  output: The character is A
       %d         printf("The boy is %d years old \n", y)
                  output: The boy is 15 years old
       %s         printf("My name is %s \n", $1)
                  output: My name is Bob Smith
       %f         printf("z is %5.3f \n", z)
                  output: z is 2.300
                                                           
FORMAT SPECIFIER MODIFIERS

between “%” and letter %10s
      %7d
      %10.4f
      %-20s
meaning:
     width of field, field is printed right justified
     precision: number of digits after decimal point
     “-” will left justify

SPRINTF:   FORMATTING TEXT
Syntax:- sprintf(format-string, var1, var2, …)
   Works like printf, but does not produce output
   Instead it returns formatted string

Example:
  {
      text = sprintf("1: %d – 2: %d", $1, $2)
      print text
  }


BUILTIN FUNCTIONS:-
1) tolower(string) :- returns a copy of string, with each upper-case character converted to lower-case. Nonalphabetic
    characters are left unchanged.
Example: tolower("MiXeD cAsE 123") will returns "mixed case 123"
2) toupper(string) :- returns a copy of string, with each lower-case character converted to upper-case.

Examples:-
LIST OF PRODUCTS
103:sway bar:49.99
101:propeller:104.99
104:fishing line:0.99
113:premium fish bait:1.00
106:cup holder:2.49
107:cooler:14.89
112:boat cover:120.00
109:transom:199.00
110:pulley:9.88
105:mirror:4.99
108:wheel:49.99
111:lock:31.00
102:trailer hitch:97.95          

OUTPUT :-
Marine Parts R Us
Main catalog
Part-id name                     price
======================================
101     propeller               104.99
102     trailer hitch            97.95
103     sway bar                 49.99
104     fishing line              0.99
105     mirror                    4.99
106     cup holder                2.49
107     cooler                   14.89
108     wheel                    49.99
109     transom                 199.00
110     pulley                    9.88
111     lock                     31.00
112     boat cover              120.00
113     premium fish bait         1.00
======================================
Catalog has 13 parts


BEGIN {
          FS= ":"
          print "Marine Parts R Us"
          print "Main catalog"
          print "Part-id\tname\t\t\t price"
          print "======================================"
}
{
          printf("%3d\t%-20s\t%6.2f\n", $1, $2, $3)
          count++
}                                           is output sorted ?
END {
          print "======================================"
          print "Catalog has " count " parts"
}

ARRAY:-
 awk allows one-dimensional arrays to store strings or numbers
 index can be number or string
   array need not be declared
     its size
     its elements
   array elements are created when first used
       initialized to 0 or “”
Syntax:
 arrayName[index] = value

Examples:
 list[1] = "one"
 list[2] = "three"
 list["other"] = "oh my !"
 

ASSOCIATIVE ARRAYS
   awk arrays can use string as index

AWK BUILTIN SPLIT FUNCTION
split(string, array, fieldsep)
     divides string into pieces separated by fieldsep, and stores the pieces in array
     if the fieldsep is omitted, the value of FS is used.

Example:
 split("auto-da-fe", a, "-")

  sets the contents of the array a as follows:
    a[1] = "auto"
    a[2] = "da"
    a[3] = "fe"

EXAMPLE: PROCESS SALES DATA
   input file:
   output:                        
       summary of category sales


AWK PROGRAM:-

Examples:-
% cat sales.awk
{
        deptSales[$2] += $3
}
END {
        for (x in deptSales)
                print x, deptSales[x]
}
% awk –f sales.awk sales

DELETE ARRAY ENTRY

The delete function can be used to delete an element from an array.

Format:- delete array_name [index]

Example:- delete deptSales["supplies"]

AWK CONTROL STRUCTURES
  1) Conditional
       if-else
   2) Repetition
       for with counter
       for with array index
       while
       do-while
       break, 

       continue

IF   Statement:-
Syntax:
     if (conditional expression)
       statement-1
     else
       statement-2
Example:
     if ( NR < 3 )
       print $2
     else
       print $3

FOR   Loop:-
Syntax:- for (initialization; limit-test; update)
         statement
Example:
  for (i = 1; i <= NR; i++)
  {
        total += $i
        count++
  }

FOR Loop for array:-
Syntax:- for (var in array)
         statement
Example:
  for (x in deptSales)
  {
        print x, deptSales[x]
  }

WHILE LOOP:-
Syntax:- while (logical expression)
         statement

Example:
  i = 1
  while (i <= NF)
  {
        print i, $i
        i++
  }

DO-WHILE Loop:-
Syntax:
    do
        statement
    while (condition)
statement is executed at least once, even if condition is false at the beginning
Example:
    i = 1
    do {
      print $0
      i++
    } while (i <= 10)                          

Loop Control Statements:-
   break :- exits loop
   continue :- skips rest of current iteration, continues with next iteration

LOOP CONTROL EXAMPLE:-
for (x = 0; x <   20; x++) {
  if ( array[x]   > 100) continue
  printf "%d ",   x
  if ( array[x]   < 0 ) break
}

No comments:

Post a Comment