example of use of $FILENAME ? 
Author Message
 example of use of $FILENAME ?

% I have an awk script that does exactly what I want it to... with 1
% exception...
%
% here it is... (usage is flightsort.awk <input_file>)
%
% #!/bin/sh
% awk -F, '{
%       print $1","$2","$3","$4
%       }' $* |
% sort -t, -k4 |
% awk -F, '
% $4 == FlightLine {
%        }
% $4 != FlightLine {
%       FlightLine = $4
%     print $4 > "index.txt"
% }'

[the exception, is he'd like "index.txt" to be replaced by the name of
the input file with the extension "ind"]

The problem is the file name isn't around any more by the time you get
to the second awk.

I'd suggest getting rid of the pipeline. Keep an array with the FlightLines
you've seen already to determine whether to print, and pipe to sort from
within awk to get sorted output. Your original script becomes

 #!/usr/bin/awk -F, -f
 !($4 in FlightLines) {
   print $1","$2","$3","$4 | "sort -t, -k4 > index.txt"
   FlightLines[$4]
 }

To write to many output files, you simply fiddle with the sort command:
 #!/usr/bin/awk -F, -f
 inputFile != FILENAME {
    if (length(sort)) {
      close(sort)
    }
    sort = "sort -t, -k4 > " FILENAME ".ind"
    inputFile = FILENAME
    delete FlightLines
 }

 !($4 in FlightLines) {
   print $1","$2","$3","$4 | sort
   FlightLines[$4]
 }

Finally, I think the print line is slightly cleaner-looking if you set
OFS to , at some point:

 #!/usr/bin/awk -F, -f
 BEGIN { OFS = "," }
 inputFile != FILENAME {
    if (length(sort)) {
      close(sort)
    }
    sort = "sort -t, -k4 > " FILENAME ".ind"
    inputFile = FILENAME
    delete FlightLines
 }

 !($4 in FlightLines) {
   print $1, $2, $3, $4 | sort
   FlightLines[$4]
 }

You could argue that's not an improvement.

If this is just a simplification of a more difficult problem that can't
be easily solved by pulling everything together into a single awk script,
I'd suggest adding the input file name to each input line, and stripping
it out again at print time. You can then have a print line like this:
 { print $2,$3,$4,$5 > $1 ".ind" }

I've seen some awk implementations where you can't concatenate at that point,
and you have to do something like this:

 { fn=$1 ".ind"; print $2,$3,$4,$5 > fn }

Hope that helps.
--

Patrick TJ McPhee
East York  Canada



Wed, 24 Oct 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using variable filename(!FILENAME)

2. Urgent Help on Deleting a filename using tcl (script) in an application (without using rm filename (in unix))

3. How to Specify Variable FileName(!FileName)In Report

4. Long Filenames okay for physical filenames???

5. REQUIRES filename (NEEDS filename)

6. DOS 8.3 filenames (how to remove long filenames header)

7. Library os.path.isfile(filename) - long filenames error.

8. Adding PC Filename Extensions to Macintosh Filenames

9. Using extracted text to create a new filename

10. Using Variable Filenames and Paths

11. Should I GPF using CLOSE(filename)

12. long filenames using microfocus on NT, please help.

 

 
Powered by phpBB® Forum Software