Page 1 of 1

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

Posted: 28 Feb 2019 15:53
by lalat06bag
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.

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

Posted: 28 Feb 2019 18:06
by Squashman
And what is the output supposed to look like?

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

Posted: 28 Feb 2019 19:42
by lalat06bag
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

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

Posted: 28 Feb 2019 20:06
by Aacini
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

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

Posted: 28 Feb 2019 20:27
by lalat06bag
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.

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

Posted: 28 Feb 2019 20:31
by ShadowThief
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

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

Posted: 28 Feb 2019 20:50
by lalat06bag
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.

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

Posted: 01 Mar 2019 09:10
by aGerman
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

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

Posted: 01 Mar 2019 09:59
by Aacini
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

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

Posted: 04 Mar 2019 14:59
by lalat06bag
Thank you a lot , Team! it worked perfectly fine.