Removing blank spaces and tabs from line without messing with line endings

I am trying to remove all spaces on a file without messing with line endings. This is to be run from inside a bash script.

I have tried all the solutions here:

They work from the command line but when I try them from the bash script the final file is a mess, completely destroyed.

File is using UNIX file endings.

Any clues?

Asked By: Duck


You should be able to use tr, but not as specified on the page your link points to as that includes the removal of newline and carriage return. What you should do is:

tr -d " t" < infile.txt > outfile.txt
Answered By: Anthon

You can pass the [[:blank:]] character class to tr to delete spaces and tabs but retain newlines

<file tr -d '[[:blank:]]'
Answered By: iruvar
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.