How to eliminate the spaces in a line in a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

How to eliminate the spaces in a line in a text file

#1 Post by lalat06bag » 28 Feb 2019 15:53

H,
I need help in building a program for a text file.
The text file has few lines. I want to count each line, if any line length exceeds 40 characters, i need to eliminate the spaces in between the words and just keep a single space between the words. If the line started with spaces too and it exceeds the character limit, the spaces should be eliminated.

example of the text file: please read the dash as spaces below.

blue red-------------------------------------------------------------------fox
apple banana orange
green-------------------------------------------------------------------- house
----------------------------------------------------------------- red orange
--------------------------------------------------------------------------------------------------1000----------------------------dollar

Please any once see this.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to eliminate the spaces in a line in a text file

#2 Post by Squashman » 28 Feb 2019 18:06

And what is the output supposed to look like?

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: How to eliminate the spaces in a line in a text file

#3 Post by lalat06bag » 28 Feb 2019 19:42

Hi,

blue red-------------------------------------------------------------------fox
apple banana orange
green-------------------------------------------------------------------- house
----------------------------------------------------------------- red orange
--------------------------------------------------------------------------------------------------1000----------------------------dollar

out of above 4 lines, say line 3, 4 and 5 are exceeding 40 characters in each line.
say:
line 1 < 40 chars, so leave as is.
Line 2 < 40 chars, leave as is.
line 3 >= 40 chars, replace all the spaces with single space.
line 4 >=40 chars, so as line 3
same line 5 as well.

Output in the txt file would be as below.


blue red-------------------------------------------------------------------fox
apple banana orange
green house
red orange
1000 dollar

Thanks, Lal

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to eliminate the spaces in a line in a text file

#4 Post by Aacini » 28 Feb 2019 20:06

Several comments about your "question":
  • Your example is incorrect. Line 1 is 78 chars, not < 40
  • I suppose that with "replace all the spaces with single space" you really want to say "replace all groups of adjacent spaces with single space". If so, then:
  • Lines 4 and 5 in the output example are wrong, because they should include one space at beginning...
I strongly encourage you to carefully read the first topic in this forum before you post any further "task requirement"..

Antonio

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: How to eliminate the spaces in a line in a text file

#5 Post by lalat06bag » 28 Feb 2019 20:27

1. I have this requirements. Please dont count by the exact characters given in the example. I am in a critical situation and need this urgently.
2. in example, i have said "say".
3. yes, space at the beginning would be added in line 4 and 5.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to eliminate the spaces in a line in a text file

#6 Post by ShadowThief » 28 Feb 2019 20:31

Aacini wrote:
28 Feb 2019 20:06
  • Lines 4 and 5 in the output example are wrong, because they should include one space at beginning...
lalat06bag wrote:
28 Feb 2019 15:53
If the line started with spaces too and it exceeds the character limit, the spaces should be eliminated.
These don't match

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: How to eliminate the spaces in a line in a text file

#7 Post by lalat06bag » 28 Feb 2019 20:50

to avoid confusion, i am giving the exact input in the text file and exact output would be. Sorry, my example confused.

Please read the dashes as blank spaces. text file name : test.txt

Input:
blue-red---------------------fox
apple-banana-orange
green-------------------------------------------------------------------- house
------------------------------------------------------------------ red-orange
--------------------------------------------------------------------------------------------------1000----------------------------dollar

Output:
blue-red---------------------fox
apple-banana-orange
green-house
-red-orange
-1000-dollar

explanation:
Line 1 in the put <= 40 chars, so would be left as is.
Line 2 in the put <= 40 chars, so would be left as is.
Line 3 in the put >= 40 chars, so the spaces would be replaced by single space in between the words and before the word if there is any space.
Line 1 in the put <= 40 chars, so the spaces would be replaced by single space in between the words and before if there is any space.
Line 1 in the put <= 40 chars, so the spaces would be replaced by single space in between the words and before if there is any space.

kindly see.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to eliminate the spaces in a line in a text file

#8 Post by aGerman » 01 Mar 2019 09:10

This is only slightly tested. Just give it a go.

Code: Select all

@echo off &setlocal

set "infile=test.txt"
set "outfile=test2.txt"

setlocal EnableDelayedExpansion
set "spcs= "
for /l %%i in (1 1 12) do set "spcs=!spcs!!spcs!"

<"!infile!" >"!outfile!" (
  for /f %%a in ('type "!infile!"^|find /c /v ""') do for /l %%b in (1 1 %%a) do (
    set "line=" &set /p "line="

    if defined line (
      set "str=A!line!"
      set "len=0"
      for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
      )

      if !len! gtr 40 (
        set /a "k=4096"
        for /l %%i in (1 1 13) do (
          for %%k in (!k!) do (
            for %%s in ("!spcs:~,%%k!") do set "line=!line:%%~s= !"
          )
          set /a "k=k/2+1"
        )
      )
    )
    echo(!line!
  )
)
endlocal
Steffen

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to eliminate the spaces in a line in a text file

#9 Post by Aacini » 01 Mar 2019 09:59

Simpler:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in (input.txt) do (
   set "input=%%a"
   if "!input:~40!" equ "" (
      echo(!input!
   ) else (
      set "output=!input:~0,1!"
      if "!output!" neq " " set "output="
      for %%b in (!input!) do set "output=!output!%%b "
      echo(!output:~0,-1!
   )
)) > output.txt
Antonio

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: How to eliminate the spaces in a line in a text file

#10 Post by lalat06bag » 04 Mar 2019 14:59

Thank you a lot , Team! it worked perfectly fine.

Post Reply