[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Option for read to handle incomplete last line
From: |
Martin Schulte |
Subject: |
Option for read to handle incomplete last line |
Date: |
Sun, 24 Oct 2021 11:03:58 +0200 |
Hello,
my apologies if there's a much easier solution for the following problem - in
this case please let me know!
>From time to time a run into troubles when reading a file with a while-read
>loop where the last "line" is not terminated with a newline.
I found an ugly looking solution (probably relying on undocumented features)
when reading the whole line into one variable (see below).
The attached patch for bash-5.1.8 will add an -E option to the builtin read
that will avoid the problem.
To test it run the patched bash on the following script:
>>>
input=$'Line 1\nLine 2\nIncomplete line 3'
echo "while read line"
printf '%s' "$input" | while read line; do printf ' %s\n' "$line"; done
echo "while read line || [[ \$line != '' ]]"
printf '%s' "$input" | while read line || [[ $line != '' ]]; do printf ' %s\n'
"$line"; done
echo "while read -E line"
printf '%s' "$input" | while read -E line; do printf ' %s\n' "$line"; done
echo "while read -E line with no characters between last \\n and EOF"
printf '%s\n' "$input" | sed 's/Incomplete l/L/' | while read -E line; do
printf ' %s\n' "$line"; done
<<<
The patch has not been tested intensely - first I would like to hear if I'm on
a sensible way.
Best regards
Martin
read-E.patch
Description: Text Data
- Option for read to handle incomplete last line,
Martin Schulte <=