Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
AWK
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
AWK
(section)
Add topic