Bash has a way of using while IFS= read -r ; do . . . done < input.txt
structure , to print exact contents of the file, but one of the peculiar features here is that if you omit IFS=
, the leading spaces won't be read. Thus, we can do the following:
$ cat input.txt
Text
$ while read -r line; do printf "%s\n" "$line"; done < input.txt
Text
And to replace the text of the original file, one can modify the command slightly like so:
$ cat input.txt
Text
$ while read -r line; do printf "%s\n" "$line"; done < input.txt > temp.txt ; mv temp.txt input.txt
$ cat input.txt
Text
We can also use parameter expansion to delete commas as well
$ cat input.txt
Text, text,
$ while read -r line; do printf "%s\n" "${line//,}"; done < input.txt
Text text
tr -s" "
could also be simulated via modifying printf
slightly
$ cat input.txt
Text, text,
$ while read -r line; do printf "%s " ${line//,};printf "\n"; done < input.txt
Text text