Awk
Download
1 / 18

awk - PowerPoint PPT Presentation


  • 98 Views
  • Uploaded on

awk. Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology [email protected] What is " awk "?. awk = programming language text/string manipulation within shell script useful for input data in records/fields. Typical uses.

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 ' awk' - daisy


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

awk

Dr. Tran, Van Hoai

Faculty of Computer Science and Engineering HCMC Uni. of Technology

[email protected]


What is awk
What is "awk"?

  • awk = programming language

    • text/string manipulation within shell script

    • useful for input data in records/fields


Typical uses
Typical uses

  • Perform arithmetic and string operations

  • Use loops and conditionals

  • Produce formatted report

  • Process UNIX command arguments

  • Execute UNIX commands from script


Examples 1
Examples (1)

  • How to print the first three fields of each record on separate lines

  • Can we do by shell script and "cut" ?

  • We can do by "awk" in one command

    awk -F: '{ print $1; print $2; print $3 }' /etc/passwd


Syntax
Syntax

  • Command forms

awk [options] 'script' var=value file(s)

awk [options] -f scriptfile var=value file(s)

  • Variable assignment on command-line

  • "value" can be

    • string, numeric constant

    • shell variable ($name)

    • command substitution (`cmd`)


Options
Options

  • Standard options

  • Advanced options (see man pages)


Patterns and procedures
Patterns and procedures

  • Script is

    pattern { action }

  • Both are optional

    • pattern missing, apply to all lines

    • action missing, matched line is printed




Procedures
Procedures

  • Separated by newline, semicolons

  • Contained in "{}"

  • Command groups


Example 2
Example (2)

  • Print first field of each line

    { print $1 }

  • Print all lines containing pattern

    /pattern/

  • Print first field of lines containing pattern

    /pattern/ { print $1 }

  • Select record having > 2 fields

    NF > 2


Example 3
Example (3)

  • Print only lines which first field matches URGENT

    $1 ~ /URGENT/ { print $3, $2 }

  • Print number of lines matching pattern

    /pattern/ {x++}

    END {print x}

  • Sum up column 2 and print the total

    {total += $2} END { print total}


Example 4
Example (4)

  • Print lines containing < 20 characters

    length($0) < 20

  • Print lines of 7 fields and beginning with "Name:"

    NF==7 && /^Name:/


Example 5
Example (5)

  • Print fields in reverse order one per line

    {

    for (i=NF; i>=1; i--)

    print $i;

    }




Awk functions and commands
awk functions and commands

  • Control flow

    • break, continue, do/while, exit, for, if/else, return, while

  • Arithmetic

    • rand, sin, cos,…

  • String

  • IO

    • print, printf, getline,…


is NEXT