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!
== Commands == AWK commands are the statements that are substituted for ''action'' in the examples above. AWK commands can include function calls, variable assignments, calculations, or any combination thereof. AWK contains built-in support for many functions; many more are provided by the various flavors of AWK. Also, some flavors support the inclusion of [[dynamically linked library|dynamically linked libraries]], which can also provide more functions. === The ''print'' command === The ''print'' command is used to output text. The output text is always terminated with a predefined string called the output record separator (ORS) whose default value is a newline. The simplest form of this command is: ; <code>print</code> :This displays the contents of the current record. In AWK, records are broken down into ''fields'', and these can be displayed separately: ; <code>print $1</code> : Displays the first field of the current record ; <code>print $1, $3</code> : Displays the first and third fields of the current record, separated by a predefined string called the output field separator (OFS) whose default value is a single space character Although these fields (''$X'') may bear resemblance to variables (the $ symbol indicates variables in the usual Unix shells and in [[Perl]]), they actually refer to the fields of the current record. A special case, ''$0'', refers to the entire record. In fact, the commands "<code>print</code>" and "<code>print $0</code>" are identical in functionality. The ''print'' command can also display the results of calculations and/or function calls: <syntaxhighlight lang="awk"> /regex_pattern/ { # Actions to perform in the event the record (line) matches the above regex_pattern print 3+2 print foobar(3) print foobar(variable) print sin(3-2) } </syntaxhighlight> Output may be sent to a file: <syntaxhighlight lang="awk"> /regex_pattern/ { # Actions to perform in the event the record (line) matches the above regex_pattern print "expression" > "file name" } </syntaxhighlight> or through a [[pipe (Unix)|pipe]]: <syntaxhighlight lang="awk"> /regex_pattern/ { # Actions to perform in the event the record (line) matches the above regex_pattern print "expression" | "command" } </syntaxhighlight> === Built-in variables === AWK's built-in variables include the field variables: $1, $2, $3, and so on ($0 represents the entire record). They hold the text or values in the individual text-fields in a record. Other variables include: * <code>NR</code>: Number of Records. Keeps a current count of the number of input records read so far from all data files. It starts at zero, but is never automatically reset to zero.<ref name="GNU.org Records">{{Cite book|chapter-url=https://www.gnu.org/software/gawk/manual/html_node/Records.html#index-FNR-variable|chapter=Records|url=https://www.gnu.org/software/gawk/manual/|title=GAWK: Effective AWK Programming: A Userβs Guide for GNU Awk|date=September 2024|edition=5.3|access-date=2025-01-24}}</ref> * <code>FNR</code>: File Number of Records. Keeps a current count of the number of input records read so far ''in the current file.'' This variable is automatically reset to zero each time a new file is started.<ref name="GNU.org Records" /> * <code>NF</code>: Number of Fields. Contains the number of fields in the current input record. The last field in the input record can be designated by $NF, the 2nd-to-last field by $(NF-1), the 3rd-to-last field by $(NF-2), etc. * <code>FILENAME</code>: Contains the name of the current input-file. * <code>FS</code>: Field Separator. Contains the "field separator" used to divide fields in the input record. The default, "white space", allows any sequence of space and tab characters. FS can be reassigned with another character or character sequence to change the field separator. * <code>RS</code>: Record Separator. Stores the current "record separator" character. Since, by default, an input line is the input record, the default record separator character is a "newline". * <code>OFS</code>: Output Field Separator. Stores the "output field separator", which separates the fields when awk prints them. The default is a "space" character. * <code>ORS</code>: Output Record Separator. Stores the "output record separator", which separates the output records when awk prints them. The default is a "newline" character. * <code>OFMT</code>: Output Format. Stores the format for numeric output. The default format is "%.6g". === Variables and syntax === Variable names can use any of the characters [A-Za-z0-9_], with the exception of language keywords, and cannot begin with a numeric digit. The operators ''+ - * /'' represent addition, subtraction, multiplication, and division, respectively. For string [[concatenation]], simply place two variables (or string constants) next to each other. It is optional to use a space in between if string constants are involved, but two variable names placed adjacent to each other require a space in between. Double quotes [[delimit]] string constants. Statements need not end with semicolons. Finally, comments can be added to programs by using ''#'' as the first character on a line, or behind a command or sequence of commands. === User-defined functions === In a format similar to [[C (programming language)|C]], function definitions consist of the keyword <code>function</code>, the function name, argument names and the function body. Here is an example of a function. <syntaxhighlight lang="awk"> function add_three(number) { return number + 3 } </syntaxhighlight> This statement can be invoked as follows: <syntaxhighlight lang="awk"> (pattern) { print add_three(36) # Outputs '''39''' } </syntaxhighlight> Functions can have variables that are in the local scope. The names of these are added to the end of the argument list, though values for these should be omitted when calling the function. It is convention to add some [[whitespace character|whitespace]] in the argument list before the local variables, to indicate where the parameters end and the local variables begin.
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)