awk print redirection to variable file name

awk, file, shell script, shell scripting, shell scripts, unix scripting, unix scripting basics, variable

Login to Reply

Thread Tools Search this Thread
# 1  
Old 02-29-2008
awk print redirection to variable file name


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?
# 2  
Old 02-29-2008
can you try this:

awk -v value=$variable '{ print $17 > "output_value.txt" }' input
# 3  
Old 02-29-2008
donot qoute the variable in the filename

Hi Nazeeb, Smilie

check the below code

awk '{ print $17 > "output_"$25".txt" }' input
# 4  
Old 02-29-2008
Thanks krao!!!
awk -F, '{ print $1 > "output_"$2".txt"}' 1

works fine!

nawk -FS=, '{ print $1 > "output_"$2".txt"}' 1

generates error Smilie
nawk: syntax error at source line 1
context is
{ print $1 > >>> "output_"$2 <<< ".txt"}
nawk: illegal statement at source line 1

i think I'll manage it Smilie
# 5  
Old 02-29-2008
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
# 6  
Old 02-29-2008
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
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:

I think I need a way around to what i am trying to achieve.
# 7  
Old 12-03-2008
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

and then execute that.
Login to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk file redirection issue Corona688 Shell Programming and Scripting 5 12-12-2017 07:33 AM
awk to lookup stored variable in file and print matching line cmccabe Shell Programming and Scripting 2 01-17-2017 09:38 AM
awk print columns and variable Ads89 Shell Programming and Scripting 9 01-30-2016 04:32 AM
Variable won't print out - awk myaa02 Shell Programming and Scripting 1 08-20-2013 10:03 PM
awk print variable then fields in variable SkySmart Shell Programming and Scripting 4 04-06-2013 06:11 PM
little problem of file redirection (awk) beca123456 UNIX for Dummies Questions & Answers 10 09-29-2012 06:42 AM
awk print using a variable h0ujun Shell Programming and Scripting 3 02-08-2012 01:55 AM
awk print variable zorrox Shell Programming and Scripting 7 05-08-2011 12:25 AM
awk output redirection to file learnscript Shell Programming and Scripting 6 03-03-2010 09:35 PM
How to print a value in the variable using awk ? jisha Shell Programming and Scripting 1 01-14-2008 07:45 AM
print variable in file using awk kamel.seg Shell Programming and Scripting 3 01-03-2008 06:43 AM
awk two file redirection kamel.seg Shell Programming and Scripting 1 12-18-2007 02:17 PM
variable inside awk '{print $c}' Tártaro Shell Programming and Scripting 3 04-13-2007 09:22 AM
can awk print column using a variable ?? jambesh Shell Programming and Scripting 36 09-26-2006 08:39 AM
print variable in awk kazanoova2 Shell Programming and Scripting 8 05-17-2004 07:30 PM
All times are GMT -4. The time now is 06:31 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
Show Password

Not a Forum Member?
Forgot Password?