Mailinglist Archive: opensuse (1318 mails)
| < Previous | Next > |
Re: [opensuse] BASH - while read; howto read last line in file without blank line at end?
- From: Randall R Schulz <rschulz@xxxxxxxxx>
- Date: Sat, 1 Nov 2008 07:05:25 -0700
- Message-id: <200811010705.25574.rschulz@xxxxxxxxx>
On Friday 31 October 2008 20:51, David C. Rankin wrote:
I can reproduce your result if the the last line (non-blank) has no
trailing newline. Specifically, if the "lastline" line you show does
not have a newline, then the read loop ignores it.
I guess it's a BASH oddity. Vim will warn you when you open such a file.
I'm not sure if it will allow you to create one (I used a different
editor to create the test data I used).
Also, in this particular case, at least, none of the curly braces
(inside the quoted string or around the while loop) are necessary.
Randall Schulz
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
Listmates,
I'm stumped on another simple BASH problem. How do I read the last
line of a file with a while loop without requiring a blank line at
the end of the file? Simple example I'm stuck on:
{ while read alias url; do
echo -e "${alias}\t${url}"
done } < ~/linux/scripts/config/repos
The repos flat text file (shown with --- above and below to show no
blank line at the end) is:
---
[sample data elided]
---
If there is no blank line at the end of the file, the while loop
reads the "oss-src" line, but does not read the "lastline". If there
is a blank line at the end, it reads "lastline" just fine. What is
the trick to be able to read the last line of the repos file
regardless of whether there is a blank at the end or not?
I can reproduce your result if the the last line (non-blank) has no
trailing newline. Specifically, if the "lastline" line you show does
not have a newline, then the read loop ignores it.
I guess it's a BASH oddity. Vim will warn you when you open such a file.
I'm not sure if it will allow you to create one (I used a different
editor to create the test data I used).
Also, in this particular case, at least, none of the curly braces
(inside the quoted string or around the while loop) are necessary.
--
David C. Rankin
Randall Schulz
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx
| < Previous | Next > |