3

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.

Ken Gentle
  • 13,277
  • 2
  • 41
  • 49
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110

1 Answers1

6

i think

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

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

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212