MYSYSAD.COM

Delete Extra Spaces Between Words in a File

· Shell & text · Files & disks

I had a file peppered with extra spaces, which were between the terms (chars need to be contiguous). I could read it but it was fairly annoying. I used the tr utility to delete most of the extra spaces and then vi'ed the file to rid it of the rest. To accomplish this task, here's an example using the tr utility -- nothing fancy.

Since Blogger is displaying this test file without the extra spaces, please take my word for it.
# cat filename
This is a test. We will delete the extra spaces.
This can be accomplished with the UNIX tr utility.
# cat filename | tr -s ' ' ' '
This is a test. We will delete the extra spaces.
This can be accomplished with the UNIX tr utility.

Output to newfilename
# cat filename | tr -s ' ' ' ' > newfilename

View newfilename contents
# more newfilename
This is a test. We will delete the extra spaces.
This can be accomplished with the UNIX tr utility.

Per a reader's request, here's a picture similar to the aforementioned without Blogger compressing the spaces.