Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
AWK
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Structure of AWK programs == [[File:POSIX awk.pdf|thumb]] {{Blockquote|AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.|author= Alfred V. Aho<ref>{{cite web |first=Naomi |last=Hamilton |url=https://www.computerworld.com/article/2535126/the-a-z-of-programming-languages--awk.html |title=The A-Z of Programming Languages: AWK |date=May 30, 2008 |work=[[Computerworld]] |access-date=2008-12-12 |archive-date=2020-02-01 |archive-url=https://web.archive.org/web/20200201095859/https://www.computerworld.com/article/2535126/the-a-z-of-programming-languages--awk.html |url-status=live }}</ref>}} An AWK program is a series of pattern action pairs, written as: <syntaxhighlight lang="awk"> condition { action } condition { action } ... </syntaxhighlight> where ''condition'' is typically an expression and ''action'' is a series of commands. The input is split into records, where by default records are separated by newline characters so that the input is split into lines. The program tests each record against each of the conditions in turn, and executes the ''action'' for each expression that is true. Either the condition or the action may be omitted. The condition defaults to matching every record. The default action is to print the record. This is the same pattern-action structure as sed. In addition to a simple AWK expression, such as <code>foo == 1</code> or <code>/^foo/</code>, the condition can be <code>BEGIN</code> or <code>END</code> causing the action to be executed before or after all records have been read, or ''pattern1, pattern2'' which matches the range of records starting with a record that matches ''pattern1'' up to and including the record that matches ''pattern2'' before again trying to match against ''pattern1'' on subsequent lines. In addition to normal arithmetic and logical operators, AWK expressions include the tilde operator, <code>~</code>, which matches a [[regular expression]] against a string. As handy [[syntactic sugar]], ''/regexp/'' without using the tilde operator matches against the current record; this syntax derives from [[sed]], which in turn inherited it from the [[Ed (text editor)|ed]] editor, where <code>/</code> is used for searching. This syntax of using slashes as [[delimiter]]s for regular expressions was subsequently adopted by [[Perl]] and [[ECMAScript]], and is now common. The tilde operator was also adopted by Perl.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)