Read file using while loop not reading last line

I have written a script to read the file line by line.
It is reading and printing the lines.
But it is coming out of loop before reading last line.
So I am not able to print last line.
How do I solve it.

it should read

post your file here

It's old bag/feature. "While read" doesn't work when the last line of a file doesn't end with newline char. It reads the line but returns false (non-zero) value.

You can use this:

DONE=false
until $DONE; do
    read s || DONE=true
    # you code
done < FILE

i tried in solaris. It is working fine without new line character

bash-3.00$ while read line; do echo $line; done < /tmp/myfile
bc
de
fg
bash-3.00$ cat /tmp/myfile
bc
de
fg

Without the last newline your prompt should be like so:

% echo -n 'one
tho' > myfile
% cat myfile
one
tho%    

yes.. yazu, you are correct :slight_smile:

thanks for the info

awk '{print NR" : "$0}' abc.txt

Dependin whether wanting to number empty line as well or not

cat -n abc.txt
cat -b abc.txt

...Ooops