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
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.
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.
Summary: I am writing an awk statement, and need to hold a variable for use until that variable is updated. Sample data and file - *actual is medical data so can not post GCAR101 red fast leather 1 3.2 l eng...
Summary: How can I pass an awk variable to the system command. i have a string variable (like abc.txt) taken from a awk whose extension .txt is to be removed. I am using basename command to remove it. basename...
Summary: i've seen a solution for this with single line awk, but what if i want to extract the variables from the awk END section back to the shell? any ideas? thanks. ...