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

后端 未结 3 955
野的像风
野的像风 2021-01-20 15:21

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

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

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-20 16:09

    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...

提交回复
热议问题