Help required:Awk Error

Help required:Awk Error

am 17.04.2008 21:04:36 von rejithomas.d

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

Re: Help required:Awk Error

am 17.04.2008 21:40:06 von PK

On Thursday 17 April 2008 21:04, rejithomas.d@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.

Re: Help required:Awk Error

am 17.04.2008 22:14:14 von Michael Tosch

rejithomas.d@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 @ hp : com

Re: Help required:Awk Error

am 17.04.2008 22:35:01 von Bill Marcum

On 2008-04-17, Michael Tosch 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
>

Re: Help required:Awk Error

am 17.04.2008 22:39:06 von Janis Papanagnou

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

Awk accepts as well variables which contain valid filenames.

Janis

Re: Help required:Awk Error

am 17.04.2008 22:50:56 von PK

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.

Re: Help required:Awk Error

am 18.04.2008 18:08:02 von Icarus Sparry

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 {....