Gawk usage: Difference between revisions

From wikinotes
Line 10: Line 10:
= 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 'BEIGN {print 1.5 / 2}'  # 0.75
awk 'BEGIN {print 1.5 / 2}'  # 0.75
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Commands -->
</blockquote><!-- Commands -->

Revision as of 01:07, 19 July 2021

Awk is a versatile glue language, and it can be used in a few ways.

Stdin

echo 'aa bb cc' | awk '{print $2}'  # bb

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 script

Or use a shebang to indicate script runs in awk

#!/usr/bin/env awk -f
# foo.awk

and run it directly

./foo.awk