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

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.

Answers


i think

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

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


Need Your Help

Easier way to find substrings within strings (Obj C)

objective-c string substring

I'm just wondering if there is a simpler way to find a substring within a string in Objective C.

pandas - how to sort the result of DataFrame.groupby.mean()?

python pandas

I am trying to figure out how to sort a results of pandas.DataFrame.groupby.mean() in a smart way.