Operating System - HP-UX
1748058 Members
5006 Online
108758 Solutions
New Discussion юеВ

awk - end of file from getline call

 
SOLVED
Go to solution
Raynald Boucher
Super Advisor

awk - end of file from getline call

Hello all

We are using a small awk script to capture any entry in file_a that is not in file_b.
Both files are sorted on the same order.
The program fails when the last entries of file_b are missing.

The call is: awk -f awk_script_name file_a
file_b is matched using commands:
getline <"file_b">0
LASTFS=$1

How can I make LASTFS=999999999 when I reach end-of-file on file_b?

Thanks all

RayB
6 REPLIES 6
OldSchool
Honored Contributor
Solution

Re: awk - end of file from getline call

"file_b is matched using commands:

getline <"file_b">0
LASTFS=$1"

Not really. getline reads one line from "file_b". typically it would be used w/ and if test or a while loop, as in

if (getline <"file_b" > 0)
{do stuff}

or

while (getline<"file_b" >0) {
do stuff}

that (at least to me) implies that you are reading thru file_b once for every line in file_a. If so, thats not real efficient.

why write this in awk when grep will do it?:

grep -v -f file_b filea

or

comm filea fileb -2 -3???
Raynald Boucher
Super Advisor

Re: awk - end of file from getline call

Yahoo...
You solved my immediate problem: I will use "comm -23 file_a file_b" instead because it is faster than grep.
The original was written using awk so I tried to fix it first.

However, I would still like to know how to trap this getline EOF for future use.
I tried reading examining $?, $0, NF etc but it looks like those aren't reset when EOF is reached. I also tried using if within the do while loop but nothing has worked.
I'd like all that effort not to be wasted for nothing.

Here is the actual code:

BEGIN{
FS="." # remove the .doc and .DOC
LASTFS=0
}
{
# The database list is on std. input
db=$1
# print db, " from db"
if ( db < LASTFS ){
print db, "is missing" >>"missing_forms"
}
else{
if ( db == LASTFS ){
print db, " is ok" >>"present_forms"
}
else{
do {
getline <"files_numbers_only">0
# print $1, " read from file"
LASTFS=$1
}while(LASTFS
if( db == LASTFS ){
print db, " is ok" >>"present_forms"
}
else{
print db, " is missing">>"missing_forms"
}
}
}
}

Michael Mike Reaser
Valued Contributor

Re: awk - end of file from getline call

Just a small tweak should be all that's necessary. In place of your getline on the file

getline <"files_numbers_only">0
# print $1, " read from file"
LASTFS=$1

use

a=getline <"files_numbers_only">0
if(a==1)
{
LASTFS=$1
}
else
{
LASTFS=999999999
}

The getline function returns a value indicating success or failure - just trap that returned value, then make your decisions based on the status of getline.
There's no place like 127.0.0.1

HP-Server-Literate since 1979
OldSchool
Honored Contributor

Re: awk - end of file from getline call

What Mike R said!

ok, so....

x=(getline < "yourfile") ;

sets "x" = return code of getline and $x to whatever got read from the file.

in looking at the code you posted, it looks like you are referring to this segment:

else
{
if ( db == LASTFS )
{
print db, " is ok" >>"present_forms"
}
else
{
do
{
getline <"files_numbers_only">0
# print $1, " read from file"
LASTFS=$1
}while(LASTFS
if( db == LASTFS )

this: "getline <"files_numbers_only">0",

I would think this needs to be either

if (getline<"file_numbers_only" >0)
{LASTFS = 9999999;
}

---or---

x=(getline<"file_numbers_only");
if ( x > 0) ........


note that this construct: getline <"files_numbers_only">0 is half of a logical test,
the 'getline <"files_numbers_only"' tells getline to redirect its input to the named file. The " > 0" should have been part of a logical test.....
Raynald Boucher
Super Advisor

Re: awk - end of file from getline call

Thanks Mike and OldSchool.
Your solutions work fine.

I finaly found the reason why none of the changes I was attempting were making any difference:
I was executing the original script after every change I made to my work copy.

My forehead is very flat right noow!

Take care and Thanks again.

RayB
OldSchool
Honored Contributor

Re: awk - end of file from getline call

sounds like a case of caffeine deficiency to me... :-)