Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk print redirection to variable file name

Tags
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

Hello,

i need to redirect the output of print to a variable file name:

Code:
#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,

check the below code

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

works fine!

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

generates error
Quote:
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
# 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
content:
Quote:
1,2,3
4,5,6
7,8,9
i tried so many things (some of which looked funny too):
Code:
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.
# 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
....
EOF

and then execute that.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print contents of file when the file path is in a variable? wolfv Shell Programming and Scripting 2 02-15-2018 01:44 PM
awk to lookup stored variable in file and print matching line cmccabe Shell Programming and Scripting 2 01-17-2017 08:38 AM
To print value for a $variable inside a $variable or file abcabc1103 Shell Programming and Scripting 5 02-16-2014 11:24 PM
awk print variable then fields in variable SkySmart Shell Programming and Scripting 4 04-06-2013 05:11 PM
Output redirection of c binary file to a file in shell script is failing Maya29988 Shell Programming and Scripting 2 08-22-2012 10:59 AM
How to print variable value using file. bhavesh.sapra Shell Programming and Scripting 6 10-30-2009 05:24 AM
Standard output redirection from a variable rm-r Shell Programming and Scripting 4 11-03-2008 09:50 AM
print variable in file using awk kamel.seg Shell Programming and Scripting 3 01-03-2008 05:43 AM
awk two file redirection kamel.seg Shell Programming and Scripting 1 12-18-2007 01:17 PM
File redirection namtab UNIX for Dummies Questions & Answers 4 01-10-2002 10:48 AM


All times are GMT -4. The time now is 07:49 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password