Accessing file name 
Author Message
 Accessing file name

Hi,
    Is there a way of accessing the input filename within an awk program,
which is run on command line as % awk -f filename. I need to write stuff
to files which are formed by appending the filename.

Thanks for your help.

Vardhan.

--

#-----------------#-----------------#------------------#------------------#
 Vardhan Walavalkar                             <-----                        /|
 215, Harvard Ave #3,                   <-----                       TWaNg!  / |    
 Allston, MA02134.                                 <-----            \ |
  http://www.*-*-*.com/ ;                       <-----     \|
#-----------------#-----------------#------------------#------------------#

        This theory is worthless. It is not even wrong!
                                                        - Wolfgang Pauli



Sun, 05 Mar 2000 03:00:00 GMT  
 Accessing file name

Quote:

> Is there a way of accessing the input filename within an awk program,
> which is run on command line as % awk -f filename. I need to write stuff
> to files which are formed by appending the filename.

Use the built-in variable FILENAME. For example:

{
    ...
    print "stuff I need to write to files" >FILENAME ".new"
    ...

Quote:
}

Or, better:

{
    ...
    if (FNR == 1)
        newfile = FILENAME ".new"
    print "stuff I need to write to files" >newfile
    ...

Quote:
}

(From p. 35 of _The AWK Programming Language_:  "FILENAME is set each time
a new file is read.")

By the way, this

    { if (FNR == 1) newfile = FILENAME ".new" }

can be expressed as this

    FNR == 1 { newfile = FILENAME ".new" }

or as this

    { FNR == 1 && newfile = FILENAME ".new" }

More information than you wanted or needed, but I felt compelled to
share that with you. ;-)

--
Jim Monty

Tempe, Arizona USA



Mon, 06 Mar 2000 03:00:00 GMT  
 Accessing file name

:     { if (FNR == 1) newfile = FILENAME ".new" }

: can be expressed as this

:     FNR == 1 { newfile = FILENAME ".new" }

: or as this

:     { FNR == 1 && newfile = FILENAME ".new" }

: More information than you wanted or needed, but I felt compelled to
: share that with you. ;-)

Thanks for the useful info :-) though Im guessing what FNR is, the number
of files to be processed? also my original statement was wrong, it should have
been awk -f file.awk FILENAME, so FILENAME is the argument.. Im wondering
what happens when there are more than 1 files as arguments.. does FILENAME
store the current file to be processed?

Vardhan.

: --
: Jim Monty

: Tempe, Arizona USA

--

#-----------------#-----------------#------------------#------------------#
 Vardhan Walavalkar                             <-----                        /|
 215, Harvard Ave #3,                   <-----                       TWaNg!  / |    
 Allston, MA02134.                                 <-----            \ |
 http://crs-www.bu.edu/crs/Vardhan/                         <-----     \|
#-----------------#-----------------#------------------#------------------#

        This theory is worthless. It is not even wrong!
                                                        - Wolfgang Pauli



Thu, 09 Mar 2000 03:00:00 GMT  
 Accessing file name



Quote:

>:     { if (FNR == 1) newfile = FILENAME ".new" }

>: can be expressed as this

>:     FNR == 1 { newfile = FILENAME ".new" }

>: or as this

>:     { FNR == 1 && newfile = FILENAME ".new" }

>: More information than you wanted or needed, but I felt compelled to
>: share that with you. ;-)

>Thanks for the useful info :-) though Im guessing what FNR is, the number
>of files to be processed?

No it is the record number of the current file
Watchit, I had an awk that didn't deal properly with FNR

Quote:
> also my original statement was wrong, it should have
>been awk -f file.awk FILENAME, so FILENAME is the argument.. Im wondering
>what happens when there are more than 1 files as arguments.. does FILENAME
>store the current file to be processed?

Yes!
If you try   awk '{print FNR, FILENAME}' a*
you will see how it works on all files beginning with a*

Rgds
Mark
--
Mark Katz
ISPC, London - Innovation in data-delivery tools
Tel: (44) 181-455 4665, Fax (44) 181-458 9554
** Visit our website on http://www.efiche.com/efiche **



Sat, 11 Mar 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. retaining a accessing file name

2. DOS File Lookup - just the file name, not the path AND file name

3. Access a file when I only know its name

4. Exclusive file access or serial access to external file

5. How can I know the .EXE file name, file date, file size

6. mkdir same name as file name

7. file name vs. real name

8. How do I access a file's last accessed attribute

9. Accessing MS Access Files from clarion

10. Two threads accessing the same file, different access modes

11. Accessing MS ACCESS Files how to

12. Accessing Clipper Files from Access

 

 
Powered by phpBB® Forum Software