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...")
 
Line 17: Line 17:
= Scripting =
= Scripting =
<blockquote>
<blockquote>
run an awk file from interpreter
<syntaxhighlight lang="bash">
<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">
./foo.awk
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Scripting -->
</blockquote><!-- Scripting -->

Revision as of 01:02, 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 'BEIGN {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