Home > Archive > AWK > February 2008 > FILENAME never gets a value when running gawk from a batch file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
FILENAME never gets a value when running gawk from a batch file
|
|
| di98mase 2008-02-29, 3:58 am |
| 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
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 == 1 { print "Filename processed:",FILENAME }
:
:
But both examples with the same result "Filename processed:-".
I cant see why this should not work? Is it because I use a input
parameter to the batch file?
/di98mase
| |
|
| On 29 Feb., 09:28, di98mase <di98m...@hotmail.com> 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 == 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
| |
| di98mase 2008-02-29, 3:58 am |
| On 29 Feb, 09:43, Janis <janis_papanag...@hotmail.com> wrote:
> On 29 Feb., 09:28, di98mase <di98m...@hotmail.com> wrote:
>
>
>
>
> 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.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> '-' is the convention to denote standard input.
>
>
>
>
> 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
>
>
>
>
>
>
> - Visa citerad text -- D=F6lj citerad text -
>
> - Visa citerad text -
It works! just by removing the '<' it works! Nice!
Is there drawbacks/benefits passing the input file as an arguement vs
using a stream?
Thanks Janis!
/di98mase
| |
| Ed Morton 2008-02-29, 8:00 am |
|
On 2/29/2008 3:27 AM, di98mase wrote:
> On 29 Feb, 09:43, Janis <janis_papanag...@hotmail.com> wrote:
>=20
[color=darkred]
[color=darkred]
>=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.
|
|
|
|
|