Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why is FILENAME variable unavailable ?!

Status
Not open for further replies.

Baraka69

Programmer
Apr 22, 2003
32
DE
I'm trying to use the built-in variable called FILENAME.

According to the manual pages (HP-UX-11.00) for awk it says: "FILENAME A pathname of the current input file."

My awk script:
BEGIN {
printf("%s (", FILENAME)
}
{
# print a * every 2'000 lines of input
if ( statusCounter == 2000 ) {
printf("*")
statusCounter = 0
}
else statusCounter++
}
END {
printf(")")
}

The desired output should look like this:
mypath1/myfile1name (***********)
mypath2/myfile2name (**************)
mypath3/myfile3name (*********)

Instead I get:
(***********)
(**************)
(*********)

I do not understand where the fault is or why I don't get the filename displayed. Any ideas?
 
the 'FILENAME' is NOT available in the BEGIN block.
To get around the problem you can do the following - just adding this as the FIRST clause after the BEGIN block (not tested):

FNR == 1 { printf("%s\n%s", (NR==1) ? "" : ")", FILENAME)}
 
Thanks a lot. Though I did not get round to testing your code, I found a different solution myself:

I am calling my AWK script from a shellscript and there I simply added an extra variable "-v myfilename=$filename".

e.g.

find . -name access_log.gz | while read filename
do
gzcat $filename | awk -v myfilename=$filename -f myAWK
done


One more thing the FILENAME does not seem to be available in the END{} either (did not work for me). Maybe that is just true for the first file (in a long row of files being processed). Not sure, but if it was only available withing the actual AWk script it would be of no use in this case (not for me).
 
BTW, awk can take multimple files as it's input - you don't have to pass it one file at the time.
If you take a look at my previous post, you'll see how the processing of multiple files is segregated inside the script.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for pointing that out.

I knew that, but for me it would be counter productive.

I have many files and need the output seperate.

If I feed multiple files, they will be worked upon as a big bunch and fed into my script and I would get one output file.

I need one output file per input file, therefore I cannot use multiple file input.

I have a different script using multiple input and that works just fine.

Do not get me wrong, I find it nice that you provide further information based on the question/sample script. In this case I actually wanted it to be non-multiple. ;-)
 
understood.

<just an FYI>
You can 'capture' the 'current' file in the clause that I've provided like so (adjusting for the desired localtion of the output files):

FNR == 1 { curFile=FILENAME &quot;.output&quot;; printf(&quot;%s\n%s&quot;, (NR==1) ? &quot;&quot; : &quot;)&quot;, FILENAME) > curFile}

and reuse the 'curFile' in your subsequent 'printf' statements redirecting it output. That way you'll get multiple output files.

</just an FYI>

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I've just tried the following on HP-UX 11.00 and they don't work...
[tt]
awk 'BEGIN {print FILENAME}' < file1

cat file1|awk 'BEGIN {print FILENAME}'
[/tt]
But this one does work...
[tt]
awk 'BEGIN {print FILENAME}' file1
[/tt]
So, if you want to use the FILENAME built-in variable, you have to specify the input file and not use pipes or input redirection.
 
interesting - I get the same on Solaris with nawk.

BTW, if you pass multiple files, nawk's ONLY prints out the first one:

nawk 'BEGIN { print FILENAME }' *.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
FYI, from 'man nawk':

FILENAME A pathname of the current input file. Inside a
BEGIN action the value is undefined. Inside an END
action the value is the name of the last input
file processed.


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Unfortunately we only have plain AWK installed (neither nawk nor gawk).

The pipeing seems to be the problem in my case, because neither in the BEGIN nor the END I have FILENAME available.

As I postet earlier, I solve the problem by using &quot;-v myFileName=$myFileName&quot; in my shell script.

The task I need to do is plow through several subdirectories and find all instances of access_log.gz, pipe them through my awk script (doing some statistics on hits and how often a single ip-address hits within a given 5 minute period), output the statistics into a file (per input I need one output file) named according to the date of the data in the input date.

I have it all up and running.

Even the status screen print (which this thread and my question was all about) is running fine. It prints the input file ( every 2000 lines processed a * and in the end ) output file.

I thank you all for your help and expertise in this matter!

All I have to work on now is the remaining problem of some imput lines being too long, but since I aready have an idea, this will only be a matter of time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top