I'm curious about how to set command-line options in awk script, like -F for field separator.
(我很好奇如何在awk脚本中设置命令行选项,例如-F作为字段分隔符。)
I try to write the shebang line like(我试着像这样写shebang行)
#!/usr/bin/awk -F ":" -f
and get the following error:
(并得到以下错误:)
awk: 1: unexpected character '.'
For this example, I can do with
(对于这个例子,我可以)
BEGIN {FS=":"}
but I still want to know a way to set all those options.
(但我仍然想知道一种设置所有这些选项的方法。)
Thanks in advance.(提前致谢。)
EDIT:
(编辑:)
let's use another example that should be easy to test.
(让我们使用另一个易于测试的示例。)
inputfile:(输入文件:)
1
2
3
4
test.awk:
(test.awk:)
#!/usr/bin/awk -d -f
{num += $1}
END { print num}
run
(跑)
/usr/bin/awk -d -f test.awk inputfile
will get 10
and generate a file called awkvars.out with some awk global variables in it.
(将得到10
并生成一个名为awkvars.out的文件,其中包含一些awk全局变量。)
(但)
./test.awk inputfile
will get
(会得到)
awk: cmd. line:1: ./test.awk
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: ./test.awk
awk: cmd. line:1: ^ unterminated regexp
if I remove '-d' from shebang line,
(如果我从shebang行中删除“ -d”,)
./test.awk inputfile
will normally output 10.
(通常会输出10。)
My question is that whether there is a way to write "-d" in test.awk file to generate awkvars.out file?
(我的问题是,是否可以在test.awk文件中写入“ -d”以生成awkvars.out文件?)
ask by Zhu translate from so