printing filenames 
Author Message
 printing filenames

Hello,

I am wondering whether or not it is possible to use awk to print the
content of a list of filenames followed by the filename. This would
be possible with a shell script but awk concatenates all files into
one single file before use making the FILENAME variable not as useful:

awk '{ print }; END { print FILENAME }' filename1 filename2 ...

does not do the job. Is a shell script with a for loop the only way
to do this?

Thanks,

Neil



Tue, 24 Dec 2002 03:00:00 GMT  
 printing filenames


Quote:

>Hello,

>I am wondering whether or not it is possible to use awk to print the
>content of a list of filenames followed by the filename. This would
>be possible with a shell script but awk concatenates all files into
>one single file before use making the FILENAME variable not as useful:

>awk '{ print }; END { print FILENAME }' filename1 filename2 ...

>does not do the job. Is a shell script with a for loop the only way
>to do this?

No, here's one way to do it:

awk 'FNR==1 && save_filename {print save_filename}
     FNR==1 {save_filename=FILENAME}
     {print}
     END{print save_filename}' infile infile1 infile2

It happens to use the fact that save_filename is automatically
initialized to zero, and once set to a non-null string it becomes
a logical one.  That causes the first line to print save_filename
before each new file.

I suppose you could do it like this too:

awk 'FNR==1 {if(save_filename) {print save_filename};
             save_filename=FILENAME}
     {print}
     END{print save_filename}' infile infile1 infile2

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 25 Dec 2002 03:00:00 GMT  
 printing filenames

Quote:


> > I am wondering whether or not it is possible to use awk to print the
> > content of a list of filenames followed by the filename. This would
> > be possible with a shell script but awk concatenates all files into
> > one single file before use making the FILENAME variable not as useful:

> > awk '{ print }; END { print FILENAME }' filename1 filename2 ...

> > does not do the job. Is a shell script with a for loop the only way
> > to do this?

> No, here's one way to do it:

> awk 'FNR==1 && save_filename {print save_filename}
>      FNR==1 {save_filename=FILENAME}
>      {print}
>      END{print save_filename}' infile infile1 infile2

> It happens to use the fact that save_filename is automatically
> initialized to zero, and once set to a non-null string it becomes
> a logical one.

This is inaccurate.

Quote:
> That causes the first line to print save_filename before each
> new file.

> I suppose you could do it like this too:

> awk 'FNR==1 {if(save_filename) {print save_filename};
>        save_filename=FILENAME}
>      {print}
>      END{print save_filename}' infile infile1 infile2

It's really only necessary to compare two variables: the built-in
variable FILENAME and the user-defined variable named, for example,
filename, which is strategically reinitialized to the value of
FILENAME each time it changes.

{
    if (filename != FILENAME) {
        if (filename) {
            print filename
        }
        filename = FILENAME
    }
    print

Quote:
}

END {
    if (filename) {
        print filename
    }

Quote:
}

Just as the "if (filename) ..." test is required in the main
pattern-action rule to prevent a spurious empty filename value from
being printed at the beginning of the ouput, it's also necessary
in the END rule to prevent a spurious empty filename value from
being printed when no input file argument is specified.

Quote:
> Eat Healthy
> Stay Fit
> Die Anyway

Eat healthFULLy (no flesh foods). Stay fit. Feel better and live
a longer, more satisfying life.

--
Jim Monty

Tempe, Arizona USA



Wed, 25 Dec 2002 03:00:00 GMT  
 printing filenames

gawk '!(FILENAME in names){names[FILENAME]=1};{print};\
END{for(i in names){printf("%s ",i)};print ""}'

got to love the easy ones
jennifer
--

Quote:

> Hello,

> I am wondering whether or not it is possible to use awk to print the
> content of a list of filenames followed by the filename. This would
> be possible with a shell script but awk concatenates all files into
> one single file before use making the FILENAME variable not as useful:

> awk '{ print }; END { print FILENAME }' filename1 filename2 ...

> does not do the job. Is a shell script with a for loop the only way
> to do this?

> Thanks,

> Neil



Fri, 27 Dec 2002 03:00:00 GMT  
 printing filenames


Quote:

>gawk '!(FILENAME in names){names[FILENAME]=1};{print};\
>END{for(i in names){printf("%s ",i)};print ""}'

>got to love the easy ones
>jennifer

That prints the filenames after all the files are printed, which
could be done more easily by just printing the arguments to the script
without the array stuff.  Your solution also fails to list
any duplicated file names.

Even this would work without the array stuff:

gawk 'FNR==1 {list=list " " FILENAME} {print}
END{print substr(list,2)}'

But I think he wanted the filename printed after each file was printed
instead of your interpretation.  That's how I read it anyway.

This does that:

awk 'FNR==1 {if(name) {print name}; name=FILENAME} {print}
     END{print name}' infile infile1 infile2

Chuck Demas
Needham, Mass.

Quote:
>--

>> Hello,

>> I am wondering whether or not it is possible to use awk to print the
>> content of a list of filenames followed by the filename. This would
>> be possible with a shell script but awk concatenates all files into
>> one single file before use making the FILENAME variable not as useful:

>> awk '{ print }; END { print FILENAME }' filename1 filename2 ...

>> does not do the job. Is a shell script with a for loop the only way
>> to do this?

>> Thanks,

>> Neil

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 27 Dec 2002 03:00:00 GMT  
 printing filenames
point taken about repeated filenames, missed that twist...

but the I think the original question wanted all the
lines, and then the list of the files they came from.

I did toy with adding the names to a buffer then dumping
the buffer at the end of input (is there a varable for the command
line contents?).  like the use of FNR though - with the buffer idea
you could just use FNR==1 {namebuf= namebuf " " FILENAME} and
END {print "contents of files:" namebuf}
(note the clever use of the "extra" leading space in namebuf :)

*grin*
Jen
who is just back from a security conference - and feeling very
paranoid at the moment!

Quote:



> >gawk '!(FILENAME in names){names[FILENAME]=1};{print};\
> >END{for(i in names){printf("%s ",i)};print ""}'

> >got to love the easy ones
> >jennifer

> That prints the filenames after all the files are printed, which
> could be done more easily by just printing the arguments to the script
> without the array stuff.  Your solution also fails to list
> any duplicated file names.

> Even this would work without the array stuff:

> gawk 'FNR==1 {list=list " " FILENAME} {print}
> END{print substr(list,2)}'

> But I think he wanted the filename printed after each file was printed
> instead of your interpretation.  That's how I read it anyway.

> This does that:

> awk 'FNR==1 {if(name) {print name}; name=FILENAME} {print}
>      END{print name}' infile infile1 infile2

> Chuck Demas
> Needham, Mass.

> >--

> >> Hello,

> >> I am wondering whether or not it is possible to use awk to print the
> >> content of a list of filenames followed by the filename. This would
> >> be possible with a shell script but awk concatenates all files into
> >> one single file before use making the FILENAME variable not as useful:

> >> awk '{ print }; END { print FILENAME }' filename1 filename2 ...

> >> does not do the job. Is a shell script with a for loop the only way
> >> to do this?

> >> Thanks,

> >> Neil

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.




Fri, 27 Dec 2002 03:00:00 GMT  
 printing filenames
One trick you could use to solve this is to pass in a variable with the
filenames between the filenames:

awk '{whatever the code is}' fname=filename1 filename1 fname=filename2
filename2 ...

then you will have access to the actual filenames to print after printing
the contents of each file.
I used this a couple years ago and know it would work, however inelegant
looking.
Len

Quote:

> Hello,

> I am wondering whether or not it is possible to use awk to print the
> content of a list of filenames followed by the filename. This would
> be possible with a shell script but awk concatenates all files into
> one single file before use making the FILENAME variable not as useful:

> awk '{ print }; END { print FILENAME }' filename1 filename2 ...

> does not do the job. Is a shell script with a for loop the only way
> to do this?

> Thanks,

> Neil

--
Leonard Coop
Dept IPPC & Entomology


Sat, 28 Dec 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Printing filename

2. Printing Report To Variable DOS Filenames

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

4. Using variable filename(!FILENAME)

5. Long Filenames okay for physical filenames???

6. REQUIRES filename (NEEDS filename)

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

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

9. Adding PC Filename Extensions to Macintosh Filenames

10. PRINT PREVIEWER FREEWARE - FormView - Views HP PCL Print Output / Prints PCL to ANY WINDOWS PRINTER

11. Printing to a Pre-Printed Form

12. How to print the Total of pages to Print

 

 
Powered by phpBB® Forum Software