|
AWK occurs as general purpose computer language that is designed for even processing text based information, either inside files or information lakes. A title AWK is from either a surnames of its authors — Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan.
Awk is an lesson of the programming language that extensively uses a string datatype, associative arrays (that is, arrays indexed by key strings), and regular expressions. A power, terseness, & limitations of awk software & sed scripts inspired Larry Wall to write Perl.
Awk is one of a early information to come out within Version 7 Unix and gained popularity as a way to add computational features to a Unix pipeline.
The version of awk occurs as standard feature of about each modern Unix-like operating system available now. Implementations of awk survive when installed package for nigh wholly more operating systems.
Structure of awk programs
Typically speaking, 2 pieces of information come given to awk: the comm& file and the primary input file. The command file (which may be an actual file, or even may be involved in the command line invocation of awk) contains a series of commands which tell awk training run a input file. A primary input file is often text that is formatted somehow; it may be an actual file, or even it may be see by awk from either a standard input. The average awk program consists of the series of lines, for each one of the form
/pattern/
in which pattern occurs as regular expression and action is a command. Awk looks through the input file; after it finds the line that matches pattern, it executes a command(s) specified within action. Surrogate line forms include:
; Start out
; Prevent
; /pattern/
;
Every one forms may be involved multiple days in the command file. Lines in the command file come executed sequentially, then whenever there are 2 "BEGIN" statements, a number 1 is executed, so a 2nd, and so a rest of the lines. Start out & Prevent statements do notDevelop to exist as placed prior to & fallowing (severally) a more lines in the command file.
Awk commands
Awk commands come a statement that is substituted for action in the examples above. Awk commands may include work calls, variable assignments, calculations, or even any combination thereof. Awk contains built-inherent trend lines for several functions; several extra come provided per various flavors of awk. Likewise, a bit of flavors trend lines a inclusion of dynamically linked libraries, which can also provide extra functions.
For brevity, a enclosure curly braces ( '' ) is omitted from either these examples.
The print command
A print command is utilized to output text. A simplest form of this command is
print
This displays a contents of the todays line. Inside awk, lines come broken down into fields, & these may be displayed one by one:
; print $1
; print $1, $3
Although these fields ($X) can bear resemblance to variables (a $ symbol indicates variables around perl), it actually refer to the fields of the todays line. The favorite example, $0, refers to the entire line. In point of fact, a commands "print" & "print $0" come monovular inside functionality.
A print command can as well display a final result of calculations and/or work calls:
print 3+2
print foobar(Ternary)
print foobar(variable)
print sin(Three-Two)
Output can be sent to the file
print "expression" > "file name"
Variables, et cetera
Variable list may utilise any of the characters [A-Za-z0-9_], using a exception of language keywords. A operators + - * / come addition, subtraction, multiplication, & section, severally. For even string concatenation, only place ii variables (or string constants) next to every more, optionally sustaining the space inside between. String constants come delimited by double quotes. Statements require non prevent sustaining semicolons. Eventually, comments may be added to computer software by applying # when the number 1 character in a line.
User-defined functions
Inside the format similar to C, work definitions consist of a keyword work, a work title, argument list & the function person. On this text is an case work:
work add_three(total, temporary)
This statement may be invoked when follows:
print add_three(36) # prints 39
Functions could develop variables that come in the local scope. A listings one come added to a prevent of the argument list, though values for these should become omitted whilst calling the work. These are convention to add occasionally whitespace in a argument list prior to a local variables, sequentially to suggest in which a parameters prevent & the local variables lead off.
Sample Applications
On this button is the omnipresent "Hello world program" program written in AWK:
Run
Print completely lines hanker than Lxxx characters. Note that a default action is to print the
todays line.
length > 80
Word Count
Prevent
Total First column of input
Prevent
Word Frequency, (utilizes associative arrays)
Prevent
Awk versions and implementations
Awk was originally written around 1977, and distributed by having Version Vii Unix.
Inside 1985 its authors started expanding a language, virtually all significantly by adding user-defined functions. A language is described in the book A AWK Programming Language, published 1988, and its implementation was mass produced available inside releases of UNIX System V. To stay away from confusion using a incompatible older version, this version was another time referred to as "new awk" or even nawk. This implementation was freed under the free software license in 1996, and is however maintained by Brian Kernighan.
GNU awk, or lout, is an additional loose software system implementation. It was written prior to a original implementation became freely available, & is however widely utilized.
mawk occurs as in no time awk implimentation by Mike Brennan according to the byte code interpreter.
Downloads & farther info all about these versions come available from either a web sites employed following.
Digression
the bird emblematic of Awk (a.o. on The AWK Programming Language book handle) is the Auk.
Books
[http://cm.bell-labs.com/cm/cs/awkbook/ The AWK Programming Language'' book's webpage] - includes downloads of original implementation of Awk
[http://www.gnu.org/software/gawk/manual/html_node/index.html ''The GNU Awk User's Guide]
[http://www.oreilly.com/catalog/sed2/ sed & awk'' book homepage]
[http://www.computer-books.us/awk.php Computer-Books.us] - The collection of Awk books available gratis download.
|