Gawk: Difference between revisions

From wikinotes
No edit summary
 
(21 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.
If you are expecting to use awk across different platforms, make no assumptions.
}}
 
 
= Usage =
<blockquote>
<blockquote>
== Arbitrary Math ==
{| class="wikitable"
<blockquote>
|-
bash does not support floating point arithmetric.<br>
| official docs || https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents
awk does though!.
|-
<syntaxhighlight lang="bash">
| awk intro || http://www.funtoo.org/wiki/Awk_by_Example,_Part_1
awk "BEGIN { print(5.5 / 2) }"
|-
</syntaxhighlight>
| awk intro || http://www.math.utah.edu/docs/info/gawk_7.html
</blockquote><!-- Math -->
|-
|}
</blockquote><!-- Tutorials -->


== splitting strings ==
= Notes =
<blockquote>
<blockquote>
<source lang="bash">
{|
amixer get Master | grep 'Mono:' | awk -F "[" '{print $2}' | awk -F "]" '{print $1}'
|-
awk 'BEGIN {print (10/2)}'
| [[gawk usage]]
</source>
|-
</blockquote><!-- splitting strings -->
|}
 
</blockquote><!-- Notes -->
== multiline matching ==
<blockquote>
Awk can be used to match multiple lines. This is very very useful.
<source lang="bash">
## Find the line state UP, then print the line 2 lines afterwards
## (each 'getline' returns the next line)
 
ip addr \
  | awk -F" " '/state UP/ { getline;getline; print $0 }' \
 
</source>
</blockquote><!-- multiline matching -->
</blockquote><!-- CLI -->


= Syntax =
= Syntax =
<blockquote>
<blockquote>
== Variables ==
<blockquote>
Awk does not use a character to indicate that the current word is a variable.
Note that each line must have a ';' to indicate line-end
<source lang="awk">
$1 is the first argument passed to awk
$0 is the entire line passed to awk
var = 1;
array[1] = "abc";
result = ( (1/100) * 20 );
</source>
</blockquote><!--awk variables-->
== If Statments ==
<blockquote>
<source lang="awk">
if ( var >= 10 ) {
  print "var is larger than or equal to 10";
} else if ( var <= 5 ) {
  print "var is smaller than or equal to 5";
} else {
  print "all other cases"
}
</source>
</blockquote><!-- awk if statements -->
== Loops ==
<blockquote>
<source lang="awk">
var = 0;
while ( var < 10 ) {
  print var;
  var += 1;
}
</source>
</blockquote><!-- awk loops -->
== print, printf, sprintf ==
<blockquote>
{{ expand
| print
|
Your basic print, like echo.
Multiple items can be printed separated by a comma.
They will be output with a space between them by default.
$0 prints the entire submitted line
<source lang="bash">
echo "hello" | awk '{$0,"goodbye"}'
> hello goodbye
</source>
$1, $2, $3 etc. prints each entry separated by a token. The default
token is a single space.
<source lang="bash">
echo "one two three four" | awk '{print $1 $3}'
> one three
</source>
}}
{{ expand
| printf
|
By default, every print statement is converted to a string.
printf allows you to control the output type, and the spacing of elements
<source lang="awk">
%s    # string
%f    # floating point
%i    # integer
%E    # scientific notation
%4s  # string (padded to 4 characters)
%.4s  # string (max 4 characters, truncated if necessary)
</source>
<source lang="bash">
echo "one two three" | awk '{ printf(  "%10s %5s %-10s %s",    $1,$2,$3,"\n"  ) }'
>          one    two three
</source>
<pre>
printf statements must be encased in parentheses. The first section specifies the format.
--left alignment
%10s means that the first entry ($1 in this case) is a string and will have a
'column' 10 characters wide from the start of the word.
ex: {one.......}
--right alignment
%-10s means that the entry ($3) will have a column 10 characters wide, but starting at the end of the word.
ex: {.....three}
--integer
you could also just as easily print an integer with:
echo "10.459" | awk '{ printf( "%i", $1) }'
> 10
</pre>
}}
{{ expand
| sprintf
|
sprintf is syntactically identical to printf except that instead of printing to the standard output, it is
designed to be printed to a variable.
<source lang="bash">
echo "8.234" | awk '{var = sprintf("%f", $1); print var}'
</source>
}}
</blockquote><!-- awk print, printf, sprintf -->
== 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 variables]]
|-
| [[gawk datatypes]]
|-
| [[gawk conditionals]]
|-
| [[gawk loops]]
|-
| [[gawk print]]
|-
| [[gawk matching]]
|-
| [[gawk subprocess]]
|-
|-
| http://www.math.utah.edu/docs/info/gawk_7.html
|}
|}
 
</blockquote><!-- Syntax -->
</blockquote><!-- Scripting -->

Latest revision as of 17:34, 18 June 2022