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

浪尽此生 提交于 2019-12-06 07:36:42

问题


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

awk '{print $0 >> "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

awk -v MYVAR"somedinamicdata" '{print $0 >> "MYWAR-SomeStaticText" }' 2procfile

And the outpus should be redirected to somedinamicdata-SomeStaticText.

I know I can do it via:

awk '{print $0  }' 2procfile >> "$MYVAR-somedinamicdata"

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.

Is it possible anyhow?

Thanks in advance.


回答1:


i think

awk -v MYVAR="somedinamicdata" '{print $0 >> (MYVAR "-SomeStaticText") }' 2procfile

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



来源:https://stackoverflow.com/questions/302152/awk-redirecting-script-output-from-script-to-another-file-with-dynamic-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!