Computing.Net > Forums > Unix > awk variable for filename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk variable for filename

Reply to Message Icon

Name: anilcgowda
Date: August 10, 2005 at 20:01:32 Pacific
OS: UNIX
CPU/Ram: 1GB
Comment:

Hi,

I want to build a awk variable FILENAME to get the filename of the files that are being read by the shell script.
I want to get the filename and put it as the first field in each record in the file.

Ex: if the file is file1.txt and records are
aa,bb,cc, I want the filename in a awk variable so that I can insert that into the file and make the file look like : file.txt,aa,bb,cc.

Thanks,
Anil



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: August 11, 2005 at 06:57:53 Pacific
Reply:

Actually, FILENAME is a special variable that refers to the current file that awk is processing.

awk '{print FILENAME "," $0}' file1 file2 ...


0

Response Number 2
Name: anilcgowda
Date: August 11, 2005 at 17:38:35 Pacific
Reply:


But, the above code give the path and filename. Can I get only filename..

Thanks,
Anil


0

Response Number 3
Name: Jim Boothe
Date: August 12, 2005 at 06:22:28 Pacific
Reply:

Right, these filenames come from the awk command line, so the FILENAMEs will be exactly as you specified, which could be simple names or full-path names.

To chop those down, I was able to do a substitute command on the FILENAME variable to remove everything through the final /

sub(".*/","",FILENAME)
print FILENAME "," $0

If your awk will not let you do it that way, we could reassign FILENAME to our own variable first.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: awk variable for filename

Awk Variables www.computing.net/answers/unix/awk-variables/7120.html

can I pass an awk variable to the s www.computing.net/answers/unix/can-i-pass-an-awk-variable-to-the-s/7251.html

passing awk variables to shell www.computing.net/answers/unix/passing-awk-variables-to-shell/5865.html