Pass parameter to an awk script file

前端 未结 3 1480
走了就别回头了
走了就别回头了 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 15:50

    In awk $1 references the first field in a record not the first argument like it does in bash. You need to use ARGV for this, check out here for the offical word.

    Script:

    #!/bin/awk -f
    
    BEGIN{
        print "AWK Script"
        print ARGV[1]
    }
    

    Demo:

    $ ./script.awk "Passed in using ARGV"
    AWK Script
    Passed in using ARGV
    

提交回复
热议问题