|
|
(13 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| AWK is a scripting language that was originally designed for manipulating tables and
| | A scripting language originally designed for manipulating tables, and simple cli tools. |
| presenting them in human-readable formats. It is useful on the commandline, I'd prefer
| |
| to use a more powerful language than awk than use it for scripting.
| |
|
| |
|
| {{ WARNING | | | = Tutorials = |
| There are two main variations of the unix coreutils (Gnu/BSD), each has different parameters etc.
| | <blockquote> |
| If you are expecting to use awk across different platforms, make no assumptions.
| | {| class="wikitable" |
| }} | | |- |
| | | official docs || https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents |
| | |- |
| | | awk intro || http://www.funtoo.org/wiki/Awk_by_Example,_Part_1 |
| | |- |
| | | awk intro || http://www.math.utah.edu/docs/info/gawk_7.html |
| | |- |
| | |} |
| | </blockquote><!-- Tutorials --> |
|
| |
|
| = Notes = | | = Notes = |
| <blockquote> | | <blockquote> |
| {| class="wikitable" | | {| |
| |- | | |- |
| | [[gawk usage]] | | | [[gawk usage]] |
| | |- |
| | |} |
| | </blockquote><!-- Notes --> |
| | |
| | = Syntax = |
| | <blockquote> |
| | {| |
| |- | | |- |
| | [[gawk variables]] | | | [[gawk variables]] |
Line 24: |
Line 37: |
| | [[gawk print]] | | | [[gawk print]] |
| |- | | |- |
| |} | | | [[gawk matching]] |
| </blockquote><!-- Notes -->
| |
| | |
| = Syntax =
| |
| <blockquote>
| |
| == split ==
| |
| <blockquote>
| |
| You can't call awk -F from within an awk script. But you can use split
| |
| to tokenize within an awk script.
| |
| | |
| <source lang="awk">
| |
| split("this is my string", a, " ")
| |
| a[1] = this
| |
| a[2] = is
| |
| </source>
| |
| | |
| <syntaxhighlight lang="bash">
| |
| echo "aaa bbb cc/11/22" \
| |
| | awk '{ split($3, a, "/"); print(a[2]); }'
| |
| # 11
| |
| </syntaxhighlight>
| |
| | |
| {{ NOTE |
| |
| awk has no way of measuring size of an array. You can however use split
| |
| on a variable, and count the number of tokens
| |
| }}
| |
| {{ WARNING |
| |
| awk array indexes start at 1
| |
| }}
| |
| </blockquote><!-- awk split -->
| |
| | |
| == match ==
| |
| <blockquote>
| |
| match checks for a matching string, returns char number if found, otherwise returns a 0
| |
| | |
| <source lang="awk">
| |
| match($0, "searchterm")
| |
| </source>
| |
| | |
| <source lang="bash">
| |
| echo "abcdefg" | awk '{var=match($0, "cd"); print var}'
| |
| #> 3
| |
| | |
| echo "abcdefg" | awk '{var=match($0, "zef"); print var}'
| |
| #> 0
| |
| | |
| echo "abcdefg" | awk '{
| |
| if(match($0, "cd")) {
| |
| print "match found";
| |
| }
| |
| }'
| |
| #> match found
| |
| </source>
| |
| </blockquote><!-- awk match -->
| |
| | |
| == system ==
| |
| <blockquote>
| |
| Executes a command in shell or cmd from an awk script.
| |
| Assigning to a variable only gives return value (1,0)
| |
| <source lang="awk">
| |
| system(ls -la);
| |
| </source>
| |
| </blockquote><!--awk system-->
| |
| | |
| == math ==
| |
| <blockquote>
| |
| <source lang="awk">
| |
| var+= 1;
| |
| var=( (100/2) * 3 );
| |
| </source>
| |
| </blockquote><!-- awk math -->
| |
| | |
| = References =
| |
| {|
| |
| |- | | |- |
| | http://www.funtoo.org/wiki/Awk_by_Example,_Part_1 | | | [[gawk subprocess]] |
| |- | | |- |
| | http://www.math.utah.edu/docs/info/gawk_7.html
| |
| |} | | |} |
| | | </blockquote><!-- Syntax --> |
| </blockquote><!-- Scripting --> | |