READ KSH Built-in Misses last Line

READ KSH Built-in Misses last Line

Post by Joseph Surio » Fri, 08 Jan 1999 04:00:00



The ksh read built-in misses the last line of a file if that line doesn't
end in \n

print -n "111 222 333\naaa bbb ccc" > testfile

while read a b c
do
    print "$a $b $c"
done < testfile

This prints only: 111 222 333.

Why can't read treat EOF as EOL?   Is there a way around this without adding
a newline
to the last line?

Thanks

 
 
 

READ KSH Built-in Misses last Line

Post by Barry Margoli » Fri, 08 Jan 1999 04:00:00




>The ksh read built-in misses the last line of a file if that line doesn't
>end in \n

Lots of programs fail like this, so you should try hard not to create
line-oriented files that don't end in newline.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.

 
 
 

READ KSH Built-in Misses last Line

Post by Joseph Surio » Fri, 08 Jan 1999 04:00:00





>>The ksh read built-in misses the last line of a file if that line doesn't
>>end in \n

>Lots of programs fail like this, so you should try hard not to create
>line-oriented files that don't end in newline.

Your replies are usually very informative, but this is wishful thinking.
Sometimes you have to write programs to handle other people's data, and
sometimes getting other people to change their data is like going through a
painful ordeal, and changing the program looks a lot better.   But I guess
not much can be done about read.  Thanks.
 
 
 

READ KSH Built-in Misses last Line

Post by Barry Margoli » Fri, 08 Jan 1999 04:00:00








>>>The ksh read built-in misses the last line of a file if that line doesn't
>>>end in \n

>>Lots of programs fail like this, so you should try hard not to create
>>line-oriented files that don't end in newline.

>Your replies are usually very informative, but this is wishful thinking.
>Sometimes you have to write programs to handle other people's data, and
>sometimes getting other people to change their data is like going through a
>painful ordeal, and changing the program looks a lot better.   But I guess
>not much can be done about read.  Thanks.

Right.  Perl and C shouldn't have problems with these files, so use one of
them.  Why use ksh when it's not the appropriate tool for the job?  Shell
scripts should be used for very simple tools, IMHO; anything remotely
unusual will usually push you up to something more capable.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.

 
 
 

READ KSH Built-in Misses last Line

Post by Samuel Carlile » Fri, 08 Jan 1999 04:00:00


echo `cat testfile` | while read a b c

Quote:> do
> print "$a $b $c"
> done

echo will append a newline.  for files that end with newline, you'll have
to deal with the last empty line, but that's trivial.

samuel carliles

 
 
 

READ KSH Built-in Misses last Line

Post by brian hile » Fri, 08 Jan 1999 04:00:00



> The ksh read built-in misses the last line of a file if that line doesn't
> end in \n

{ cat testfile; print; } | while read a b c
do      print "$a $b $c"
done

If there is already a newline terminated line in the file in question,
the above script will still behave acceptably.

-Brian

 
 
 

READ KSH Built-in Misses last Line

Post by Dan Merc » Sat, 09 Jan 1999 04:00:00






>>The ksh read built-in misses the last line of a file if that line doesn't
>>end in \n

> Lots of programs fail like this, so you should try hard not to create
> line-oriented files that don't end in newline.

Of course,  that's not always in our control.  For instance,  I
am putting a graphical front end on the smbclient utility.
Smbclient is run as a co-process,  but getting the prompt has
been a *, to say the least.  Smbclient does fflush its
output,  so the data wasn't getting stuck in the pipeline. I
finally settled on changing the line end delimiter to a space and
reading the prompt in two chunks.  This,  however,  is a ksh93
enhancement.  Attempts to read the data using dd gave
inconsistent behaviour at best (including asking me to change the
blocksize to a value larger than the available memory in the  
box).

Given that ksh93 has the ability to open sockets directly,  it definitely
needs a better interface for reading data.  Question:  can a custom
builtin override an existing builtin?

Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

READ KSH Built-in Misses last Line

Post by Jon LaBad » Mon, 11 Jan 1999 04:00:00




|> echo `cat testfile` | while read a b c
|> > do
|> > print "$a $b $c"
|> > done
|>
|> echo will append a newline.  for files that end with newline, you'll have
|> to deal with the last empty line, but that's trivial.

Instead try a variant:

   echo "`cat testfile`" |
   while read input
   do
        whatever
   done

Command substitution removes any trailing newlines (if there)
and the echo adds the needed final newline.  The double quotes
preserve the interior newlines.

Note that in my tests blank lines at the end of testfile are lost.

jl
--


 4455 Province Line Road        (609) 252-0159
 Princeton, NJ  08540-4322      (609) 683-7220 (fax)