Help required:Awk Error

I am getting the following error while executing this awk script. The
$TMP_FILENAME has a valid string

echo $TMP_FILENAME; awk ' BEGIN {while ((getline < file_name) > 0)
terminal_arra
y[$0] = 1 ;print $0}' file_name=$TMP_FILENAME

/tmp/sshterminals
awk: cmd. line:1: fatal: expression for `<' redirection has null
string value


Cant I pass a variable to BEGIN block and if so what am I doing
wrong??

Regards
Reji
rejithomas.d [ Do, 17 April 2008 21:04 ] [ ID #1944252 ]

Re: Help required:Awk Error

On Thursday 17 April 2008 21:04, rejithomas.d [at] gmail.com wrote:

>
> I am getting the following error while executing this awk script. The
> $TMP_FILENAME has a valid string
>
> echo $TMP_FILENAME; awk ' BEGIN {while ((getline < file_name) > 0)
> terminal_array[$0] = 1 ;print $0}' file_name=$TMP_FILENAME

The above command is a bit strange.
Why don't you use the -v option to assign the value to file_name? And, most
important, why are you using getline?

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
PK [ Do, 17 April 2008 21:40 ] [ ID #1944253 ]

Re: Help required:Awk Error

rejithomas.d [at] gmail.com wrote:
> I am getting the following error while executing this awk script. The
> $TMP_FILENAME has a valid string
>
> echo $TMP_FILENAME; awk ' BEGIN {while ((getline < file_name) > 0)
> terminal_arra
> y[$0] = 1 ;print $0}' file_name=$TMP_FILENAME
>
> /tmp/sshterminals
> awk: cmd. line:1: fatal: expression for `<' redirection has null
> string value
>
>
> Cant I pass a variable to BEGIN block and if so what am I doing
> wrong??
>
> Regards
> Reji
>

The < operator wants a "string" constant, not a variable.
You loop in the BEGIN section?
Did you forget that awk loops around the input in its main section?
Maybe you want

awk '{terminal_array[$0]=1; print}' $TMP_FILENAME



--
Michael Tosch [at] hp : com
Michael Tosch [ Do, 17 April 2008 22:14 ] [ ID #1944254 ]

Re: Help required:Awk Error

On Thursday 17 April 2008 22:14, Michael Tosch wrote:

> The < operator wants a "string" constant, not a variable.

AFAICT all that's required is that what is after the "<" evaluaes to a
string, not that it must be a string constant.
Actually, if you do

awk -v file_name=$TMP_FILENAME ...

awk does read from the file.
I think the problem is that the assignment file_name=$TMP_FILENAME put at
the end, after the program, is "seen" by awk only in the main program, not
in the BEGIN section.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
PK [ Do, 17 April 2008 22:50 ] [ ID #1944257 ]

Re: Help required:Awk Error

On 2008-04-17, Michael Tosch <eedmit [at] NO.eed.SPAM.ericsson.PLS.se> wrote:
>
>
> The < operator wants a "string" constant, not a variable.

It can work with a variable, the problem is that the variable wasn't set
using the -v option, so it isn't set before the BEGIN block is executed.

> You loop in the BEGIN section?
> Did you forget that awk loops around the input in its main section?
> Maybe you want
>
> awk '{terminal_array[$0]=1; print}' $TMP_FILENAME
>
Bill Marcum [ Do, 17 April 2008 22:35 ] [ ID #1944258 ]

Re: Help required:Awk Error

Michael Tosch wrote:
>
> The < operator wants a "string" constant, not a variable.

Awk accepts as well variables which contain valid filenames.

Janis
Janis Papanagnou [ Do, 17 April 2008 22:39 ] [ ID #1944259 ]

Re: Help required:Awk Error

On Thu, 17 Apr 2008 12:04:36 -0700, rejithomas.d wrote:

> I am getting the following error while executing this awk script. The
> $TMP_FILENAME has a valid string
>
> echo $TMP_FILENAME; awk ' BEGIN {while ((getline < file_name) > 0)
> terminal_arra
> y[$0] = 1 ;print $0}' file_name=$TMP_FILENAME
>
> /tmp/sshterminals
> awk: cmd. line:1: fatal: expression for `<' redirection has null string
> value
>
>
> Cant I pass a variable to BEGIN block and if so what am I doing wrong??
>
> Regards
> Reji

Usually you would say

awk -vfile_name="$TMP_FILENAME" 'BEGIN {....
Icarus Sparry [ Fr, 18 April 2008 18:08 ] [ ID #1945041 ]
Linux » comp.unix.shell » Help required:Awk Error

Vorheriges Thema: Environment Files
Nächstes Thema: Wget