Friday, October 05, 2007

Delete Extra Spaces Between Words in a File

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.

3 comments:

Anjali said...

Hi...

I tried this one but not working.
Can you better take a screen shot the shell screen and put in blog.

Regards

Anjali

esofthub said...

anjali,

That's a good idea and no problem.

Roy

Toryisland said...

try
# cat -vte filename

which will show you all the extra spaces with a $

Rodney