Gawk usage: Difference between revisions
From wikinotes
(Created page with "Awk is a versatile glue language, and it can be used in a few ways. = Stdin = <blockquote> <syntaxhighlight lang="bash"> echo 'aa bb cc' | awk '{print $2}' # bb </syntaxhigh...") |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
= Stdin = | = Stdin = | ||
<blockquote> | <blockquote> | ||
== tokenizing == | |||
<blockquote> | |||
by default, each space-separated item on stdin is an argument.<br> | |||
you can use this to quickly extract space separated lines. | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
echo 'aa bb cc' | awk '{print $2}' # bb | echo 'aa bb cc' | awk '{print $2}' # bb | ||
</syntaxhighlight> | </syntaxhighlight> | ||
you can also choose the token used to split the string | |||
<syntaxhighlight lang="bash"> | |||
echo 'aa|bb|cc' | awk -F'|' '{print $2}' # bb | |||
</syntaxhighlight> | |||
</blockquote><!-- tokenizing --> | |||
== multiline matching == | |||
<blockquote> | |||
Match a line, then return N words afterwards. | |||
<syntaxhighlight lang="bash"> | |||
# Find the line state UP, then print the line 2 lines afterwards | |||
# (each 'getline' returns the next line) | |||
ip addr | awk '/state UP/ { getline; getline; print $0 }' | |||
</syntaxhighlight> | |||
</blockquote><!-- multiline matching --> | |||
</blockquote><!-- Stdin --> | </blockquote><!-- Stdin --> | ||
= Commands = | = Commands = | ||
<blockquote> | <blockquote> | ||
awk is useful on it's own, for it's ability to do floating point math in bash. | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
awk ' | awk 'BEGIN {print 1.5 / 2}' # 0.75 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Commands --> | </blockquote><!-- Commands --> | ||
Line 17: | Line 38: | ||
= Scripting = | = Scripting = | ||
<blockquote> | <blockquote> | ||
run an awk file from interpreter | |||
<syntaxhighlight lang="bash"> | |||
awk -f file.awk [arg..] # run awk script | |||
</syntaxhighlight> | |||
Or use a shebang to indicate script runs in awk | |||
<syntaxhighlight lang="bash"> | |||
#!/usr/bin/env awk -f | |||
# foo.awk | |||
</syntaxhighlight> | |||
and run it directly | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
./foo.awk | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Scripting --> | </blockquote><!-- Scripting --> |
Latest revision as of 01:12, 19 July 2021
Awk is a versatile glue language, and it can be used in a few ways.
Stdin
tokenizing
by default, each space-separated item on stdin is an argument.
you can use this to quickly extract space separated lines.echo 'aa bb cc' | awk '{print $2}' # bbyou can also choose the token used to split the string
echo 'aa|bb|cc' | awk -F'|' '{print $2}' # bbmultiline matching
Match a line, then return N words afterwards.
# Find the line state UP, then print the line 2 lines afterwards # (each 'getline' returns the next line) ip addr | awk '/state UP/ { getline; getline; print $0 }'
Commands
awk is useful on it's own, for it's ability to do floating point math in bash.
awk 'BEGIN {print 1.5 / 2}' # 0.75
Scripting
run an awk file from interpreter
awk -f file.awk [arg..] # run awk scriptOr use a shebang to indicate script runs in awk
#!/usr/bin/env awk -f # foo.awkand run it directly
./foo.awk