Reduce spaces in between 2 words in txt file

Posted on 2007-08-08
Last Modified: 2010-03-05

I have a txt file which has data like this


I want the data like this

Question by:bsharath
    LVL 70

    Expert Comment

    Load it into word  and use filnd/replace to find two spaces and replace with one space - you may need to do the find/replace all several times to get rid of all the double spaces
    LVL 10

    Expert Comment

    Or more complete:

    Do a first find and replace
    - search for the tab-character
    - replace with one space

    Then as KCTS suggested do as much as you need
    - search for two spaces
    - replace with one spac

    LVL 11

    Author Comment

    But there are a differerent of space
    One have 1 space 2,3,4,5,6,7,8,9, so on....
    LVL 10

    Expert Comment

    If you start with 1,2,3,4,5,6,7,8,9 spaces
    If you do the find and replace the first time, you will have
    1, 2,3,4,5,6,7,8 as a result,
    One more time find and replace and you have
    1, 2,3,4,5,6,7
    and do this as much as is necessary
    LVL 10

    Expert Comment

    If you have a lot of spaces, you can start with a find and replace from 5 spaces to one which you do a few times, after that you have to do the 2 to 1 only max 4 times.
    LVL 82

    Accepted Solution

    Try this; just adjust the names of the file to read and the file with the removed spaces:

    @echo off
    setlocal enabledelayedexpansion
    set InputFile=test.txt
    set OutputFile=SpacesRemoved.txt
    if exist "%OutputFile%" del "%OutputFile%"
    for /f "tokens=*" %%a in ('type "%InputFile%"') do (
      set Line=
      for %%l in (%%a) do set Line=!Line! %%l
      >>"%OutputFile%" echo !Line:~1!

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
    This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now