1 / 12

AWK

AWK. Perl without the Fluff. What is AWK?. “AWK is a convenient and expressive programming language that can be applied to a wide variety of computing and data-manipulation tasks.” - AWK’s authors AWK is ideal when large amounts of data need to be manipulated. History of AWK.

avon
Download Presentation

AWK

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AWK Perl without the Fluff

  2. What is AWK? • “AWK is a convenient and expressive programming language that can be applied to a wide variety of computing and data-manipulation tasks.” - AWK’s authors • AWK is ideal when large amounts of data need to be manipulated.

  3. History of AWK • Why is it called AWK? • Authors: • A -> Alfred Aho • W-> Peter Weinberger • K -> Brian Kernighan • Aho, Weinberger and Kernighan created AWK in 1977 at AT&T Bell Laboratories • Initial Release in 1978 for Version 7 Unix

  4. History of AWK • Over the next 10 years, numerous implemenations were created • Nawk (interpreter), gawk (interpreter), tawk (compiler for Windows), mksawk (compiler), awkc++ (translates awk to C++), awk2c (translates to C) • In 1992 POSIX standard 1003.2 was introduced to “standardize applications and user interfaces to open systems.” • POSIX 1003.2 standardized more than just AWK • It standardized a majority the Unix/Linux shell utilities used today. cp, mv, kill, chown, chmod to name a few. • Also standardized Unix/Linux shell grammer, regular expressions, etc.

  5. AWK vs. Perl • Perl can do everything AWK can and more • AWK is a simpler language that Perl • AWK is easier to learn than Perl • AWK is more widely available in the Unix/Linux environment • AWK code is quick to write • Clean and clear Perl code is better than unclear AWK code. But unclear Perl code is very difficult to understand

  6. AWK: • AWK is a special purpose language • Block structured and Functional • Can be compiled or interpreted, depending upon the implementation • Variables essentially have two values, a numeric value (floating point), and a string value. • Default values of 0 and “”

  7. AWK: • AWK decides whether to use the variable as a string or number depending on the operation currently being processed • All Arrays in AWK are associative • Loops and Conditionals • If, if-else, for, while • Special Conditionals • for (everyItem in array) • Similar to Perl’s foreach. Access to every item in an array • If (key in array) • Checks to see if the key exists in the array

  8. AWK: • Awk provides some builtin functions for both strings, and numbers • Supports user defined functions • Provides easy access to the shell environment through the ENVIRON array • Very similar to Perl’s %ENV

  9. AWK: • Input is read in as “records” • A record is split into “fields” • Each field is accessable as a variable, $0 - $n • $0 is the entire record • $1 is the first field in the record, $2 is the second … $n is the nth field in the record • Statements end with either a semicolon or newline • The basic syntax for the body of an AWK program is: pattern { action } • Both the pattern and action are optional, but one must be present

  10. What practicle application is there for AWK? • Suppose you had a file containing a list of addresses: Jane Doe 456 3rd. Ave Boise, Id 83706 John Doe 123 2nd Ave Boise, Id 83715

  11. What practicle application is there for AWK? • Say you wanted the Addresses printed out backwards. A program like this one would do that BEGIN { RS = "" ; FS = "\n" } { print "City, State and ZIP are:", $3 print "Address is:",$2 print "Name is:", $1 print "" }

  12. Conclusion: • AWK is a quick way to manipulate data from the command line or from a script • AWK is not the fanciest way to accomplish data manipulation, but it works • AWK doesn’t have all the functionality of Perl • AWK is more simplistic and easier to learn than Perl • AWK is a great tool for any shell environment (even Windows)

More Related