0

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.

1

Yes, the (\r = ^M = 0x0D) character belongs to the space "character class". Allow for it in your second regex, either with a wildcard:

sed -i.bak -E 's/[ \t]+.$/\r/' myfile

or the specific char, like

sed -i.bak -E 's/[ \t]+\r$/\r/' myfile

Should your sed not recognize the \r notation, try other. like hex \x0D or octal \015.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.