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

I know that I can redirect awk print output to another file from a script, for example:

awk '{print $0 >> "anotherfile" }' 2procfile

(I know this dummy example, but this is just an example ...)

But I need to redirect the output to another file with a dynamic name like this

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

And the redirection should be redirected to somedinamicdata-SomeStaticText.

I know I can do this through:

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

But the problem is that it is more of an 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.

Is it possible?

Thanks in advance.

+3
source share
1 answer

I think,

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

. awk .

+6

All Articles