nazeeb
February 29, 2008, 12:29am
1
Hello,
i need to redirect the output of print to a variable file name:
#This is normal
awk '{ print $17 > "output.txt" }' input
#I need something like this
awk '{ print $17 > "output_${25}.txt" }' input
how to format the output file name to contain a variable?
sank
February 29, 2008, 2:25am
2
can you try this:
awk -v value=$variable '{ print $17 > "output_value.txt" }' input
krao
February 29, 2008, 2:51am
3
Hi Nazeeb,
check the below code
awk '{ print $17 > "output_"$25".txt" }' input
nazeeb
February 29, 2008, 7:28am
4
Thanks krao!!!
awk -F, '{ print $1 > "output_"$2".txt"}' 1
works fine!
But
nawk -FS=, '{ print $1 > "output_"$2".txt"}' 1
generates error
i think I'll manage it
krao
February 29, 2008, 7:50am
5
hi nazeeb,
can u provide a sample input , in that plz specify what delimiters you are using like , = : ... etc. so that i will try to help you in nawk code
nazeeb
February 29, 2008, 9:02am
6
Hi krao,
really appreciate your conecern.
the input is just a test file to get the output working on a trial and error basis:
filename: 1
content:
i tried so many things (some of which looked funny too):
nawk -FS=, '{ print $1 > "output_'$2'.txt"}' 1
nawk -FS=, '{ print $1 > "output_\"$2\".txt"}' 1
but it produces output file name with "$" in it.
On the contrary, awk produces the following files:
output_2.txt
output_5.txt
output_8.txt
I think I need a way around to what i am trying to achieve.
jg167
December 3, 2008, 7:32pm
7
This works as long as the variable containing the filename is a positional variable. But if you are using some logic to change the file names not once per line but on some other key, I could find no way to output to a filename in a named variable. So I had to use awk to make a shell script with
cat >filename <<EOF
....
EOF
and then execute that.