在awk脚本头中指定其他标志 [英] Specify other flags in awk script header

查看:80
本文介绍了在awk脚本头中指定其他标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用#!/bin/awk -f标头编写awk脚本文件,但是我希望此脚本始终使用:作为字段分隔符.但是由于某些原因,编写#!/bin.awk -F: -f会给我带来语法错误.我还希望该脚本始终在同一文件上运行,因此我也希望对其进行硬编码.基本上,我要工作的是:

I want to write an awk script file using the #!/bin/awk -f header, but I want this script to always use : as a field separator. But for some reason writing #!/bin.awk -F: -f gives me a syntax error. I also want this script to always run on the same file, so I'd like to hardcode that as well. Basically, what I want to work is this:

#!/bin/awk -F: -f -- /etc/passwd

后面是一些awk代码

推荐答案

应该执行以下操作:

#!/bin/awk -f              # using the #!/bin/awk -f
BEGIN {
    FS=":"                 # always use : as a field separator
    ARGC=2
    ARGV[1]="/etc/passwd"  # always run on the same file
}
$3==0 {                    # followed by some awk code
    print $1
}

运行它:

$ chmod u+x program.awk
$ ./program.awk
root

这篇关于在awk脚本头中指定其他标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆