Pass parameter to an awk script file

前端 未结 3 1599
走了就别回头了
走了就别回头了 2021-02-01 15:23

If I want to pass a parameter to an awk script file, how can I do that ?

#!/usr/bin/awk -f

{print $1}

Here I want to print the first argument

3条回答
  •  甜味超标
    2021-02-01 16:03

    You can use -v as a command-line option to provide a variable to the script:

    Say we have a file script.awk like this:

    BEGIN {print "I got the var:", my_var}
    

    Then we run it like this:

    $ awk -v my_var="hello this is me" -f script.awk
    I got the var: hello this is me
    

提交回复
热议问题