Linux 将参数传递给 awk 脚本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15969861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass parameter to an awk script file
提问by Mahmoud Emam
If I want to pass a parameter to an awk script file, how can I do that ?
如果我想将参数传递给 awk 脚本文件,我该怎么做?
#!/usr/bin/awk -f
{print }
Here I want to print the first argument passed to the script from the shell, like:
在这里,我想打印从 shell 传递给脚本的第一个参数,例如:
bash-prompt> echo "test" | ./myawkscript.awk hello
bash-prompt> hello
采纳答案by Kent
your hash bang defines the script is not shell script, it is an awk script. you cannot do it in bash way within your script.
你的 hash bang 定义的脚本不是 shell 脚本,它是一个 awk 脚本。您不能在脚本中以 bash 方式执行此操作。
also, what you did : echo blah|awk ...
is notpassing paramenter, it pipes the output of echo command to another command.
还有,你做了什么:echo blah|awk ...
是不是经过paramenter,它管道echo命令的输出到另一个命令。
you could try these way below:
你可以试试下面这些方法:
echo "hello"|./foo.awk file -
or
或者
var="hello"
awk -v a="$var" -f foo.awk file
with this, you have var a
in your foo.awk, you could use it.
有了这个,你a
的 foo.awk 中有 var ,你可以使用它。
if you want to do something like shell script accept $1 $2 vars, you can write a small shellscript to wrap your awk stuff.
如果你想做一些类似 shell script 接受 $1 $2 vars 的事情,你可以写一个小的 shellscript 来包装你的 awk 东西。
EDIT
编辑
No I didn't misunderstand you.
不,我没有误解你的意思。
let's take the example:
让我们举个例子:
let's say, your x.awk
has:
比方说,你x.awk
有:
{print }
if you do :
如果你这样做:
echo "foo" | x.awk file
it is same as:
它与:
echo "foo"| awk '{print }' file
here the input for awk is only file
, your echo foo doesn't make sense. if you do:
这里 awk 的输入只是file
,你的 echo foo 没有意义。如果你这样做:
echo "foo"|awk '{print }' file -
or
echo "foo"|awk '{print }' - file
awk takes two input (arguments for awk) one is stdin one is the file, in your awk script you could:
awk 需要两个输入(awk 的参数)一个是标准输入,一个是文件,在你的 awk 脚本中,你可以:
echo "foo"|awk 'NR==FNR{print ;next}{print }' - file
this will print first foo
from your echo, then the column1 from file
of course this example does nothing actual work, just print them all.
这将首先foo
从您的回声中打印出来,然后file
当然这个示例中的 column1没有任何实际工作,只需将它们全部打印出来。
you can of course have more than two inputs, and don't check the NR and FNR, you could use the
你当然可以有两个以上的输入,不要检查 NR 和 FNR,你可以使用
ARGC The number of elements in the ARGV array.
ARGV An array of command line arguments, excluding options and the program argument, numbered from zero to ARGC-1
for example :
例如 :
echo "foo"|./x.awk file1 - file2
then your "foo" is the 2nd arg, you can get it in your x.awk by ARGV[2]
那么你的“foo”是第二个参数,你可以在你的 x.awk 中得到它 ARGV[2]
echo "foo" |x.awk file1 file2 file2 -
now it is ARGV[4] case.
现在是 ARGV[4] 情况。
I mean, your echo "foo"|..
would be stdin for awk, it could by 1st or nth "argument"/input for awk. depends on where you put the -
(stdin). You have to handle it in your awk script.
我的意思是,您echo "foo"|..
将是 awk 的标准输入,它可以是 awk 的第一个或第 n 个“参数”/输入。取决于你把-
(标准输入)放在哪里。您必须在 awk 脚本中处理它。
回答by fedorqui 'SO stop harming'
You can use -v
as a command-line optionto provide a variable to the script:
您可以将其-v
用作命令行选项来为脚本提供变量:
Say we have a file script.awklike this:
假设我们有一个像这样的文件script.awk:
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
回答by Chris Seymour
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 herefor the offical word.
Inawk
$1
引用记录中的第一个字段,而不是像 in 那样的第一个参数bash
。您需要为此使用ARGV
,请在此处查看官方词。
Script:
脚本:
#!/bin/awk -f
BEGIN{
print "AWK Script"
print ARGV[1]
}
Demo:
演示:
$ ./script.awk "Passed in using ARGV"
AWK Script
Passed in using ARGV