bash AWK:将脚本输出从脚本重定向到另一个具有动态名称的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/302152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:31:57  来源:igfitidea点击:

AWK: redirecting script output from script to another file with dynamic name

bashredirectawkgawk

提问by Zsolt Botykai

I know I can redirect awk's print output to another file from within a script, like this:

我知道我可以从脚本中将 awk 的打印输出重定向到另一个文件,如下所示:

awk '{print 
awk -v MYVAR"somedinamicdata" '{print 
awk '{print 
awk -v MYVAR="somedinamicdata" '{print ##代码## >> (MYVAR "-SomeStaticText") }' 2procfile
}' 2procfile >> "$MYVAR-somedinamicdata"
>> "MYWAR-SomeStaticText" }' 2procfile
>> "anotherfile" }' 2procfile

(I know that's dummy example, but it's just an example...)

(我知道这是一个虚拟的例子,但这只是一个例子......)

But what I need is to redirect output to another file, which has a dynamic name like this

但我需要的是将输出重定向到另一个文件,该文件具有像这样的动态名称

##代码##

And the outpus should be redirected to somedinamicdata-SomeStaticText.

并且输出应该被重定向到somedinamicdata-SomeStaticText.

I know I can do it via:

我知道我可以通过以下方式做到:

##代码##

But the problem is that it's a bigger awk script, and I have to output to several files depending on certain conditions (and this awk script is called from another bash, and it passes some dynamic variable via the -v switch... and son on.

但问题是它是一个更大的 awk 脚本,我必须根据某些条件输出到几个文件(这个 awk 脚本是从另一个 bash 调用的,它通过 -v 开关传递一些动态变量......和儿子在。

Is it possible anyhow?

有可能吗?

Thanks in advance.

提前致谢。

回答by Johannes Schaub - litb

i think

我认为

##代码##

should do it. String concatenation in awk is just put one after another.

应该这样做。awk 中的字符串连接只是一个接一个。