Click to See Complete Forum and Search --> : Removing double spaces from file?


rayburn
01-22-2005, 05:59 AM
Hello, I hope someone can help me please.

I have a large text file which has a number of errors in it. Specifically, there are instances of two spaces between words, one of which I wish to remove as they cause grep to report wrongly. Is there a simple way of doing this with sed for instance?

I would be most grateful for any suggestions as I am fairly new to Linux.

Architect
01-22-2005, 06:48 AM
Try using egrep instead of grep.

deathadder
01-22-2005, 07:56 AM
Have a look at the 'tr' command.

grep filename | tr 'double space' 'single space'

rayburn
01-22-2005, 08:41 AM
Many thanks for your replies, I will try them out and report back.

Sepero
01-22-2005, 09:01 AM
To remove all double spaces from a file with sed:
sed -i "s/ / /g" filename

rayburn
01-22-2005, 06:15 PM
Thanks for all your help, I have tried using 'tr' on a sample from the file and it appears to work successfully. I haven't tried using the sed solution yet. The code I used was this:

cat tim.txt | tr -s ' ' ' ' > tims.txt

Hope that was correct! Thanks again everyone.