Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: FILENAME ne...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 2173 of 2241
Post > Topic >>

Re: FILENAME never gets a value when running gawk from a batch file

by Ed Morton <morton@[EMAIL PROTECTED] > Feb 29, 2008 at 07:03 AM

On 2/29/2008 3:27 AM, di98mase wrote:
> On 29 Feb, 09:43, Janis <janis_papanag...@[EMAIL PROTECTED]
> wrote:
>=20
>>On 29 Feb., 09:28, di98mase <di98m...@[EMAIL PROTECTED]
> wrote:
>>
>>
>>>Hi all,
>>
>>>after a few minutes of studying the FILENAME variable I cant
>>>understand why my simple program does not work. This is what I want to=

>>>achieve:
>>
>>>I have a batch file that looks like:
>>>echo "************************ Extract all general statistics
>>>****************"
>>>gawk -f statistics.awk <..\logs\%1 > ..\results\statistics.res
>>
>>You are asking your command shell to open a file and the shell
>>connects the data stream to the standard input channel. While
>>the shell knows about the file the awk program just sees data
>>on stdin without knowledge whether the data comes from a file
>>or whether it is the output of another process that is attached
>>by a pipe.
>>
>>
>>
>>
>>
>>
>>
>>
>>>I run this using the command:
>>>process_stats.bat mylogfile.log
>>
>>>in my awk program I have tried to use this in my END statement (since
>>>the filename does not have any value in before the BEGIN is processed:=

>>
>>>END {
>>>print "Filename processed:", FILENAME;
>>
>>>}
>>
>>>I also tried:
>>
>>>BEGIN {}
>>
>>>FNR =3D=3D 1 { print "Filename processed:",FILENAME }
>>>:
>>>:
>>>But both examples with the same result "Filename processed:-".
>>
>>'-' is the convention to denote standard input.
>>
>>
>>
>>
>>>I cant see why this should not work? Is it because I use a input
>>>parameter to the batch file?
>>
>>To give programs (awk in this case) a chance to know about the
>>filename you should pass the file names as arguments to awk...
>>
>>gawk -f statistics.awk ..\logs\%1 >..\results\statistics.res
>>
>>(Mind the missing '<'.) If you want to process multiple files
>>provide a list of file names...
>>
>>gawk -f statistics.awk ..\logs\file1 ..\logs\file2 ..\logs\file3
>>
>>Janis
>>
>>
>>
>>
>>
>>
>>>/di98mase- D=F6lj citerad text -
>>
>>- Visa citerad text -- D=F6lj citerad text -
>>
>>- Visa citerad text -
>=20
>=20
> It works! just by removing the '<' it works! Nice!
>=20
> Is there drawbacks/benefits passing the input file as an arguement vs
> using a stream?

The pro for having awk open the file is that awk then knows the file name=
=2E The
con is that if there's a problem opening the file then you get whatever
diagnostic message your awk decides to produce instead of whatever your O=
S
produces, so there may be inconsistencies between the two.

Personally, I'd never let the OS open the file since that approach falls =
apart
when you want to run your script on multiple files. e.g. with one file:

	awk '...' file

you can choose to do:

	awk '...' <file

but with two:

	awk '...' file1 file2

you can't do:

	awk '...' <file1 <file2

so you'd need something like this:

	for file in file1 file2
	do
		awk '...' <"$file"
	done

which just isn't worth the effort, and if you want to know the filename, =
you'd
actually need:

	awk -v filename=3Dfile '...' <file

even just for the 1-file case.

	Ed.




 4 Posts in Topic:
FILENAME never gets a value when running gawk from a batch file
di98mase <di98mase@[EM  2008-02-29 00:28:52 
Re: FILENAME never gets a value when running gawk from a batch f
Janis <janis_papanagno  2008-02-29 00:43:41 
Re: FILENAME never gets a value when running gawk from a batch f
di98mase <di98mase@[EM  2008-02-29 01:27:55 
Re: FILENAME never gets a value when running gawk from a batch f
Ed Morton <morton@[EMA  2008-02-29 07:03:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 3:22:41 CDT 2008.