Visit Our UNIX and Linux User Community


awk print redirection to variable file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print redirection to variable file name
# 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, Smilie

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 Smilie
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 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
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #455
Difficulty: Medium
Java was originally developed at Oracle starting in December 1990.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk file redirection issue

So I'm writing a script which tries to parse human-readable addresses. Part of it is this: print $2, implode(A,1,AN," "), CITY, PROV, POST, COUNTRY, CITYCOUNT>2; CITYCOUNT is a variable between 0 and 3 counting the number of words in a city name. I'm trying to prnt 1 wherever that's greater... (5 Replies)
Discussion started by: Corona688
5 Replies

2. Shell Programming and Scripting

awk to lookup stored variable in file and print matching line

The bash bash below extracts the oldest folder from a directory and stores it in filename That result will match a line in bold in input. In the matching line there is an_xxx digit in italics that (once the leading zero is removed) will match a line in link. That is the lint to print in output.... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

4. UNIX for Dummies Questions & Answers

little problem of file redirection (awk)

I almost reach my objective (Youhouuu !!!!) But I really don't understand why it doesn't work until the end... :wall: For clarity's sake I am taking a very simple example. The operations I am doing in the script (gsub and print) really don't have any importance !!! I just matter about... (10 Replies)
Discussion started by: beca123456
10 Replies

5. Shell Programming and Scripting

awk print variable

I have list of files: ls a.pdf b.pdf c.pdf and so on... and I have a file like this: cat file1 apple mango pear and so on... I want to rename my file like this: (7 Replies)
Discussion started by: zorrox
7 Replies

6. Shell Programming and Scripting

awk output redirection to file

I have a system stat command running which generates data after 5 sec or so. I pass this data to awk and do some calculation to present the data differently. Once done now I want to pass this data to file as and when generated but doesn't work..unless the first command completes successfully.... (6 Replies)
Discussion started by: learnscript
6 Replies

7. Shell Programming and Scripting

How to print a value in the variable using awk ?

:b:Hi All, I have a part of a script below: var1="value1" awk 'BEGIN {printf("%36s \n ","value1")}' Instead of directly giving the "value1" , I need to give using "var1" in the above awk statement. Is this possible? If so, what is the modified awk command? Thanks in advance JS (1 Reply)
Discussion started by: jisha
1 Replies

8. Shell Programming and Scripting

print variable in file using awk

hi, i have store variable in $var i want to print it in $3 of file using awk. how can i do it? file : var1,var2,var3 var5,var6,var7 var8,var9,var10 . . . i want to print $var in $3 ( for example var3 or var7 or var10) thanks (3 Replies)
Discussion started by: kamel.seg
3 Replies

9. Shell Programming and Scripting

awk two file redirection

Hi, i use awk -F to print three variable delimited by comma $1 $2 $3 if $2=="" i want to extract this information missing from another file using awk -v + some process. but the problem i can't use the two awk together cause of redirection there's a solution. note: i can't use another... (1 Reply)
Discussion started by: kamel.seg
1 Replies

10. Shell Programming and Scripting

print variable in awk

i read the variable ph from file and i wanna to print it in awk. example ph=`cat tmpbatch` tail h.txt|grep "| |"|awk -F"|" '{ print "@unpdx.sql",$5 }'"$ph" i try this but it does not work (8 Replies)
Discussion started by: kazanoova2
8 Replies

Featured Tech Videos