How to eliminate the spaces in a line in a text file
Moderator: DosItHelp
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
How to eliminate the spaces in a line in a text file
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.
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
And what is the output supposed to look like?
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
Re: How to eliminate the spaces in a line in a text file
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
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
Several comments about your "question":
Antonio
- 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...
Antonio
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
Re: How to eliminate the spaces in a line in a text file
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.
2. in example, i have said "say".
3. yes, space at the beginning would be added in line 4 and 5.
-
- 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
These don't matchlalat06bag wrote: ↑28 Feb 2019 15:53If the line started with spaces too and it exceeds the character limit, the spaces should be eliminated.
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
Re: How to eliminate the spaces in a line in a text file
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.
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
This is only slightly tested. Just give it a go.
Steffen
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
Re: How to eliminate the spaces in a line in a text file
Simpler:
Antonio
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
-
- Posts: 51
- Joined: 10 Jan 2018 15:21
Re: How to eliminate the spaces in a line in a text file
Thank you a lot , Team! it worked perfectly fine.