Outputting info from awk and using the info to name the awk output file 
Author Message
 Outputting info from awk and using the info to name the awk output file

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  
 Outputting info from awk and using the info to name the awk output file


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

You can do redirection inside the script, so instead of

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

you would have
 BEGIN {FS = "\t"}
 ($1 == "N") {
   batchnum = $2
   sequence = $3
   print $0 >> batchnum
 }
 END {print "L", batchnum, sequence >> batchnum}

You'd have to do something more clever to wipe out the file the
first time you print to it.

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

That's more difficult -- you need to know the name of the output file
at the time you write it (although you can always rename it later,
and use the system() function to do that from within the END action).

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

You can say
  filename=batchnum "." sequence
  ...
  print ... >> filename

In theory, you can also say
  print ... >> batchnum "." sequence

but some buggy versions of awk don't like that.
--

Patrick TJ McPhee
East York  Canada



Sun, 08 Apr 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Naming the output file based on awk script variable

2. Copy files using AWK's output

3. run awk output thru tee inside awk

4. redirecting print output to variable name in awk

5. using awk to format ifconfig -a output

6. awk output to a new file

7. awk output into file during processing

8. How to get info from a file with sed/awk/perl

9. How to get the wanted output with AWK

10. Use awk to print syslog output to terminal with colors

11. columned output with awk?

12. Need help in formating output with AWK

 

 
Powered by phpBB® Forum Software