I am developing in linux a portable application, in which I wanted to remove trailing spaces from some source files, which have windows style line endings. The line endings should not be changed to unix.
The code -using 'sed'- was simple enough to find online:
sed -i.bak -E 's/[[:space:]]+$//' myfile
the problem is that after the operation,
diff states that ALL lines in the file (and the .bak that was generated) differ. This cannot be true of course. The specific file had only 1 line with a trailing space.
Checking with a hexdump (I used
od -x myfile) utility it is apparent that the 1st line of the original file ends in "0d0a" , whereas in the sed-edited file it ends in "0a".
I have not been able to find any information about how to perform the trailing spaces removal without affecting the line endings. Is there a way ?
if the "Carriage return" character is part of the "[[:space:]]" regex group, then perhaps this
sed -i.bak -E 's/[ \t]+$//' myfile
should work. But it does not - it creates an identical file. I have also tried the "-e" flag and even no flag at all. Still the generated file is identical to the original and no trailing space is removed.