What is the simplest way to parse line continuation characters? This seems like such a basic action that I’m surprised there’s no basic command for doing this. ‘while read’ and ‘while read -r’ loops don’t do what I want, and the easiest solution I’ve found is the sed solution below. Is there a way to do this with something basic like tr?
$ cat input
Output should be \
one line with a '\' character.
$ while read l; do echo $l; done < input
Output should be one line with a '' character.
$ while read -r l; do echo $l; done < input
Output should be \
one line with a '\' character.
$ sed '/\\$/{N; s/\\\n//;}' input
Output should be one line with a '\' character.
$ perl -0777 -pe 's/\\\n//s' input
Output should be one line with a '\' character.
If by “simplest” you mean concise and legible, I’d suggest your perl-ism with one small modification:
No need for the possibly memory intensive
... -0777 ...(whole file slurp mode) switch.If, however, by “simplest” you mean not the leaving shell, this will suffice:
(I prefer
printf "%s" $USER_INPUTtoecho $USER_INPUTbecause echo cannot portably be told to stop looking for switches, and printf is commonly a built-in anyway.)Just tuck that in a user-defined function and never be revolted by it again. Caution: this latter approach will add a trailing newline to a file which lacks one.