Naming the output file based on awk script variable 
Author Message
 Naming the output file based on awk script variable

I was wondering if it is possible to take information from a variable
within an awk script and use that information to name the output file.
For example using the following script...

awk 'BEGIN {FS = "\t"}
     { if ($1 == "N"){
         batchnum = $2
  sequence = $3
  print $0}}
     END {print "L", batchnum, sequence}' test1 > "batchnum"

Is it possible to name the output file the same as the contents within
the batchnum variable from the awk script?
If batchnum has a value of 19981201 at the end of the awk script, can I
use that value of 19981201 to name the output file.
Better yet, would it be possible to name the output file
batchnum.sequence where sequence is the value of another variable.
A sample output file might look like "19981201.test" or "19981201.real"

Any assistance with this would be greatly appreciated.
Thank you...

Terry Dunn



Sat, 07 Apr 2001 03:00:00 GMT  
 Naming the output file based on awk script variable

Quote:

> I was wondering if it is possible to take information from a variable
> within an awk script and use that information to name the output file.
> For example using the following script...

> awk 'BEGIN {FS = "\t"}
>      { if ($1 == "N"){
>          batchnum = $2
>   sequence = $3
>   print $0}}
>      END {print "L", batchnum, sequence}' test1 > "batchnum"

> Is it possible to name the output file the same as the contents within
> the batchnum variable from the awk script?
> If batchnum has a value of 19981201 at the end of the awk script, can I
> use that value of 19981201 to name the output file.
> Better yet, would it be possible to name the output file
> batchnum.sequence where sequence is the value of another variable.
> A sample output file might look like "19981201.test" or "19981201.real"

> Any assistance with this would be greatly appreciated.
> Thank you...

> Terry Dunn

The print command allows redirection to a variable which contains a
filename, so in this case you could do:

END {output_file=batchnum "." suffix ; print "L", batchnum, sequence

Quote:
>output_file }

If this was a more complicated script and you were outputting to several
files, you might need to use close(output_file) to free up the file
descriptor.  Also, if you are appending to a file, you would want to use

Quote:
>> instead of > , ala /bin/sh.

Hope this helps


Sat, 07 Apr 2001 03:00:00 GMT  
 Naming the output file based on awk script variable

Quote:

> I was wondering if it is possible to take information from a variable
> within an awk script and use that information to name the output file.

It is.

Quote:
> Is it possible to name the output file the same as the contents within
> the batchnum variable from the awk script?

Yes.

Quote:
> If batchnum has a value of 19981201 at the end of the awk script, can I
> use that value of 19981201 to name the output file?

Yes.

Quote:
> Better yet, would it be possible to name the output file
> batchnum.sequence where sequence is the value of another variable?

Yes.

Quote:
> A sample output file might look like "19981201.test" or "19981201.real"

The value that appears to the left of the redirection operator ">" can be
any expression that, when evaluated as a string, results in a valid file
name. Here's a brief demonstration:

# print "hello, world" to a file named 19981201.test
BEGIN {
    batchnum = "19981201"
    sequence = "test"
    filename = batchnum "." sequence
    print "hello, world" >filename
    exit

Quote:
}

--
Jim Monty

Tempe, Arizona USA


Sat, 07 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Outputting info from awk and using the info to name the awk output file

2. redirecting print output to variable name in awk

3. Access to Script Name Within Awk Script

4. Creating an awk script to extract other scripts from a file

5. Can't get AWK output to a PERL script

6. name of awk script

7. Save awk output as global variable

8. Using a variable name for a variable name??

9. AWK-Script to set a shell variable (Win32) ?

10. Passing command line variables to AWK in shell script

11. help for newbie: way to get the value of an awk variable in a shell script

12. passing variables to awk scripts

 

 
Powered by phpBB® Forum Software