<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.linuxquestions.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chaosless</id>
	<title>LQWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.linuxquestions.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chaosless"/>
	<link rel="alternate" type="text/html" href="https://wiki.linuxquestions.org/wiki/Special:Contributions/Chaosless"/>
	<updated>2026-04-15T12:54:09Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.37.0</generator>
	<entry>
		<id>https://wiki.linuxquestions.org/index.php?title=Awk&amp;diff=46962</id>
		<title>Awk</title>
		<link rel="alternate" type="text/html" href="https://wiki.linuxquestions.org/index.php?title=Awk&amp;diff=46962"/>
		<updated>2009-08-03T19:35:36Z</updated>

		<summary type="html">&lt;p&gt;Chaosless: /* Language structure */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[awk]] is a [[command]] for string operations. For example, it allows you to show only the second ''column'' of a file. awk is not a simple command, but rather a programming language on its own. awk and gawk (for GNU awk) are used synonymously.&lt;br /&gt;
&lt;br /&gt;
== Usage ==&lt;br /&gt;
&lt;br /&gt;
When &amp;lt;tt&amp;gt;awk&amp;lt;/tt&amp;gt; is run, it is given two forms of input, the ''program'' and the ''data''.  The program can be typed directly on the [[command line]] or stored in a file and accessed with the &amp;lt;tt&amp;gt;-f&amp;lt;/tt&amp;gt; option.  The data comes from [[file]]s listed on the command line or from [[stdin]] if none are listed.  The first example has the script on the command line with input from a file, while the second example uses an external program to create the input data and pipes into &amp;lt;tt&amp;gt;awk&amp;lt;/tt&amp;gt;, which uses an external script as the program.&lt;br /&gt;
&lt;br /&gt;
 $ awk '{ print $1; }' ''datafile''&lt;br /&gt;
&lt;br /&gt;
 $ ''makedata'' | awk -f ''myscript.awk''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;awk&amp;lt;/tt&amp;gt; scripts that are saved in files can be executed directly by placing the proper [[shebang]] line at the beginning:&lt;br /&gt;
 #!/bin/awk -f&lt;br /&gt;
'''Important note:''' use the exact path of your awk (available from typing &amp;quot;&amp;lt;tt&amp;gt;[[which]] awk&amp;lt;/tt&amp;gt;&amp;quot;) if it is not named &amp;lt;tt&amp;gt;/bin/awk&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Language structure ==&lt;br /&gt;
&lt;br /&gt;
An awk program consists of a series of statements each consisting of a ''pattern'' and an ''action''. Awk reads the input (whether [[file]]s or data [[pipe]]d from [[stdin]]) line-by-line automatically. For each line of data, if the ''pattern'' is true, the ''action'' is executed.  There are a few special patterns. The &amp;lt;tt&amp;gt;BEGIN&amp;lt;/tt&amp;gt; rule is executed first, before any input is read, and the &amp;lt;tt&amp;gt;END&amp;lt;/tt&amp;gt; rule is executed last, after the end of all input. Some complicated awk scripts consist of only a &amp;lt;tt&amp;gt;BEGIN&amp;lt;/tt&amp;gt; rule and use &amp;lt;tt&amp;gt;getline&amp;lt;/tt&amp;gt; to read the input data.  If ''pattern'' is empty, the ''action'' is always executed. If ''action'' is empty, awk echos the line.&lt;br /&gt;
&lt;br /&gt;
The pattern can be a [[regular expression]] enclosed in slashes ('/'), in which case it is considered true if the input line matches (i.e. contains matching text) the pattern. The expression &amp;lt;tt&amp;gt;/^[^#]/&amp;lt;/tt&amp;gt; would select all lines not beginning with a pound sign. The pattern could also be an awk expression, e.g. &amp;lt;tt&amp;gt;(NF&amp;amp;gt;5)&amp;lt;/tt&amp;gt; to select all lines with more than 5 words.&lt;br /&gt;
&lt;br /&gt;
Whenever a line of input is read (whether automatically or with &amp;lt;tt&amp;gt;getline&amp;lt;/tt&amp;gt; [http://www.gnu.org/software/gawk/manual/html_node/Getline.html]), the line is split into words. The first word is assigned to &amp;lt;tt&amp;gt;$1&amp;lt;/tt&amp;gt;, the second &amp;lt;tt&amp;gt;$2&amp;lt;/tt&amp;gt;, etc. This makes it easy for awk to deal with columns of data. The variable &amp;lt;tt&amp;gt;NF&amp;lt;/tt&amp;gt; is set to the number of words.  &amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt; is an awk operator, so the &amp;quot;number&amp;quot; can be the result of any expression. &amp;lt;tt&amp;gt;$NF&amp;lt;/tt&amp;gt; is the last word on the line.&lt;br /&gt;
&lt;br /&gt;
Truly remarkably power in awk can come from use of dynamic arrays, especially when combined with regular expressions.  This can allow for complex queries across many files with collection and collation of results as shown in the following example for the query &amp;quot;what are the first word of all lines and how often do they occur?&amp;quot; &lt;br /&gt;
&lt;br /&gt;
This example shows several power features:&lt;br /&gt;
  &amp;lt;ul&amp;gt;&lt;br /&gt;
    &amp;lt;li&amp;gt;selects all lines not starting with &amp;lt;tt&amp;gt;#&amp;lt;/tt&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
    &amp;lt;li&amp;gt;separates a matching line into multiple words&amp;lt;/li&amp;gt;&lt;br /&gt;
    &amp;lt;li&amp;gt;uses each word as index into wordcounts array&amp;lt;/li&amp;gt;&lt;br /&gt;
    &amp;lt;li&amp;gt;END clause, summary processing when all input is done&amp;lt;/li&amp;gt;&lt;br /&gt;
    &amp;lt;li&amp;gt;sort indices using asorti() and output counts&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/^[^#]/{&lt;br /&gt;
  w=match($0,/([a-zA-Z0-9_$]+)/,thisline);&lt;br /&gt;
  for(i=1; i&amp;lt;=w; i++) {&lt;br /&gt;
    wordcounts[toupper(thisline[i])]++;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
END {&lt;br /&gt;
  n = asorti(wordcounts, words);&lt;br /&gt;
  for (i = 1; i &amp;lt;= n; i++) {&lt;br /&gt;
    printf(&amp;quot;%14s - %4d\n&amp;quot;,words[i],wordcounts[words[i]]);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you save the above example as a file, in this case &amp;lt;tt&amp;gt;words.awk&amp;lt;/tt&amp;gt;, then scanning a group of files can be as easy as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  awk -f words.awk *.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Add more complex regex criteria, use printf() for debugging, collect different arrays of results, see split() for further parsing, these and so many more features make &amp;lt;tt&amp;gt;awk&amp;lt;/tt&amp;gt; one of the most powerful of scripting tools. &lt;br /&gt;
&lt;br /&gt;
For a complete description of the language, see the GNU awk manual [http://www.gnu.org/software/gawk/manual/html_node/index.html].&lt;br /&gt;
&lt;br /&gt;
== GNU Awk extensions ==&lt;br /&gt;
&lt;br /&gt;
Things to be careful about when using a &amp;lt;tt&amp;gt;gawk&amp;lt;/tt&amp;gt; script in a non-GNU awk include:&lt;br /&gt;
* Special files like &amp;lt;tt&amp;gt;/dev/stderr&amp;lt;/tt&amp;gt;, useful for printing error messages.&lt;br /&gt;
* The &amp;lt;tt&amp;gt;systime()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;strftime()&amp;lt;/tt&amp;gt; functions.&lt;br /&gt;
* The &amp;lt;tt&amp;gt;nextfile&amp;lt;/tt&amp;gt; statement.&lt;br /&gt;
* &amp;lt;tt&amp;gt;delete ARRA&amp;lt;/tt&amp;gt; to delete an entire array.&lt;br /&gt;
* The &amp;lt;tt&amp;gt;gensub()&amp;lt;/tt&amp;gt; function.&lt;br /&gt;
* Bidirectional pipes to coprocesses.&lt;br /&gt;
This list is not comprehensive; the gawk manual (below) has more info.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Programming#Scripting languages|Scripting languages section]]&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
&lt;br /&gt;
* [http://www.gnu.org/software/gawk/manual/html_node/index.html gawk manual] - Excellent reference, especially the sections on Reading Files, Expressions, and Functions.&lt;br /&gt;
* [http://man-wiki.net/index.php/Awk gawk man page]&lt;br /&gt;
* [http://www.tek-tips.com/threadminder.cfm?pid=271 awk forum] - when you have questions&lt;br /&gt;
* [http://sparky.rice.edu/~hartigan/awk.html How to Use AWK] - quick intro&lt;br /&gt;
* [http://www.cs.uu.nl/docs/vakken/st/nawk/nawk_toc.html awk manual] - shorter than the gawk manual above&lt;/div&gt;</summary>
		<author><name>Chaosless</name></author>
	</entry>
</feed>