Skip to Content
Linux Shell Scripting Cookbook - Third Edition
book

Linux Shell Scripting Cookbook - Third Edition

by Clif Flynt, Sarath Lakshman, Shantanu Tushar
May 2017
Beginner
552 pages
28h 47m
English
Packt Publishing
Content preview from Linux Shell Scripting Cookbook - Third Edition

Passing an external variable to awk

Using the -v argument, we can pass external values other than stdin to awk, as follows:

    $ VAR=10000
    $ echo | awk -v VARIABLE=$VAR '{ print VARIABLE }'
    10000

There is a flexible alternate method to pass many variable values from outside awk. Consider the following example:

    $ var1="Variable1" ; var2="Variable2"
    $ echo | awk '{ print v1,v2 }' v1=$var1 v2=$var2
    Variable1 Variable2

When an input is given through a file rather than standard input, use the following command:

    $ awk '{ print v1,v2 }' v1=$var1 v2=$var2 filename

In the preceding method, variables are specified as key-value pairs, separated by a space, and (v1=$var1 v2=$var2 ) as command arguments to awk soon after the BEGIN, { }, and END ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Linux Shell Scripting - Second Edition

Mastering Linux Shell Scripting - Second Edition

Mokhtar Ebrahim, Andrew Mallett

Publisher Resources

ISBN: 9781785881985