GAWK - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

GAWK

Description:

Many UNIX utilities generates rows and columns of information. ... Extract bits and pieces of data for processing. Sort data. Perform simple network communications ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 12
Provided by: Moh102
Category:

less

Transcript and Presenter's Notes

Title: GAWK


1
GAWK
  • Systems Programming Lab
  • 5th Semester

2
What is gawk?
  • This lab discusses AWK, another cornerstone of
    UNIX shell programming. There are three
    variations of AWK
  • AWK - the original from ATT
  • NAWK - A newer, improved version from ATT
  • GAWK - The Free Software foundation's version
  • Gawk is the GNU version of the commonly available
    UNIX awk program, a popular stream editor.
  • It is not an abbreviation for awkward. The word
    "AWK" is derived from the initials of the
    language's three developers A. Aho, B. W.
    Kernighan and P. Weinberger.
  • An open source programme with a large developers
    community.

Stream editors are Unix utilities that (a) parses
text files and (b) implements a programming
language which can apply textual transformations
to such files. They read input files line by line
(sequentially), applying the operation which has
been specified via the command line.
3
Why awk?
  • Programs in awk are different from programs in
    most other languages, because awk programs are
    data-driven you describe the data you want to
    work with and then what to do when you find it.
    Most other languages are procedural You have
    to describe, in great detail, every step the
    program is to take.
  • When working with procedural languages, it is
    usually much harder to clearly describe the data
    your program will process. For this reason, awk
    programs are often refreshingly easy to read and
    write.
  • Gawk interpreter makes it very easy to debug,
    even with no formal programming skills.

4
Why gawk? Cont..
  • It is an elegant and simple language.
  • It is an excellent filter and report writer.
  • Portable and standard.
  • Many UNIX utilities generates rows and columns of
    information. AWK is an excellent tool for
    processing these rows and columns, and is easier
    to use AWK than most conventional programming
    languages.
  • Fast Loads an array of 8 Million system numbers
    in less than 10 seconds.
  • AWK program is typically much smaller and faster
    to develop than a counterpart written in C.

5
Fields In AWK
6
Why gawk? Cont..
  • Using awk allows you to
  • Manage small, personal databases
  • Generate reports
  • Validate data
  • Produce indexes and perform other document
    preparation tasks
  • Experiment with algorithms that you can adapt
    later to other computer languages
  • In addition, gawk provides facilities that make
    it easy to
  • Extract bits and pieces of data for processing
  • Sort data
  • Perform simple network communications

7
Program
  • When you run awk, you specify an awk program that
    tells awk what to do. The program consists of a
    series of rules.
  • Each rule specifies one pattern to search for and
    one action to perform upon finding the pattern.
  • Syntactically, a rule consists of a pattern
    followed by an action. The action is enclosed in
    curly braces to separate it from the pattern.
    Newlines usually separate rules. Therefore, an
    awk program looks like this
  • pattern action
  • pattern action
  • ...

8
How to Run awk Programs
  • There are several ways to run an awk program. If
    the program is short, it is easiest to include it
    in the command that runs awk, like this
  • awk program input-file1 input-file2 ...
  • When the program is long, it is usually more
    convenient to put it in a file and run it with a
    command like this
  • awk -f program-file input-file1 input-file2 ...

9
Sample Code
  • awk "BEGIN print \"Dont Panic!\" "
  • Dont Panic!
  • awk print
  • Now is the time for all good men
  • Now is the time for all good men
  • Put
  • BEGIN print "Dont Panic!"
  • into the file advice.awk. Then run it using
    this command
  • awk -f ./advice.awk

10
Executable
  • Add
  • ! /bin/awk f
  • to the last program and save it.
  • Make it executable.
  • chmod x advice.awk
  • ./advice.awk
  • Dont Panic!

11
ls l ./prog.awk
  • !/bin/awk f
  • if (NF 8) print 1, 8, 5
  • else if (NF 9) print 1, 9, 5
Write a Comment
User Comments (0)
About PowerShow.com