Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: iterating through txt file lines can't read last line

Tags:

bash

bash4

while read p; do
echo $p
done < file.txt

this code can read all lines in the file.txt except the last line any ideas why. Thanks

like image 766
Hady Hallak Avatar asked May 18 '13 18:05

Hady Hallak


1 Answers

if youre in doubt about the last \n in the file, you can try:

while read p; do
echo $p
done < <(grep '' file.txt)

grep is not picky about the line endings ;)

you can use grep . file.txt for skipping empty lines...

like image 153
jm666 Avatar answered Nov 15 '22 06:11

jm666