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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getline in BEGIN section, can't seem to use variable as filename? Opti

Status
Not open for further replies.

chilecayenne

Programmer
Aug 16, 2011
4
US
Hello all,

I'm making progress on my first awk script in a LONG time.

I'm trying to now make it a bit more versitile...and send in file names as variables on the command line, rather than hard coded in, but I'm having problems.


Example...this works

BEGIN {
FS="|";
OFS="|";

while (getline < "dev_bxm_link_output.txt" > 0)
{
split ($0,bxm_rec,"|");

b_memrecno1 = bxm_rec[2];
b_memrecno2 = bxm_rec[3];
b_score = bxm_rec[5];

bxm_array[b_memrecno1,b_memrecno2] = b_score;

}

}

This works just fine when I have the file going into getline hard
coded in.
However when I try something like this:

BEGIN {
FS="|";
OFS="|";

while (getline < v_filename > 0)
{
split ($0,bxm_rec,"|");

b_memrecno1 = bxm_rec[2];
b_memrecno2 = bxm_rec[3];
b_score = bxm_rec[5];

bxm_array[b_memrecno1,b_memrecno2] = b_score;

}

}

And when calling awk sending the v_filename as a variable...it gives
me an error of:
fatal: expression for `<' redirection has null string value

I'm calling this like:

awk -f myawkfile.awk v_filename="file1.txt" file_to_process.csv

Thanks in advance,

chilecayenne

Any
 
I always use the explicit -v option to declare variables for awk as I find the var=value format after the script ambiguous; is it an input filename or a variable assignment? Besides, it doesn't always work... I'm not sure why, perhaps only supported in some flavours of awk.

Code:
awk -v v_filename="file1.txt" -f myawkfile.awk file_to_process.csv

Please post your code between [ignore]
Code:
 ...
[/ignore]
tags to make it easy on our eyes. :)

Annihilannic.
 
Besides, it doesn't always work
AFAIK, not in the BEGIN section but when the first input record is read.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top