Remove character at begin of line
Moderator: DosItHelp
Remove character at begin of line
Hello,
I have a txt file like this:
0 ABCDE67890 12345FGHIJ ...
1 12SAG3J590 FGDG45346Y ..
2 GASDFSD230 ...
3 ....
0 HFJSHSDK24323 ...
1 ....
2 ....
3 ....
I would create a batch file to search and remove all "0 " (zero+space) only at the begin of lines, preserving every other occurrences. The output should be like this:
ABCDE67890 12345FGHIJ ...
1 12SAG3J590 FGDG45346Y ..
2 GASDFSD230 ...
3 ....
HFJSHSDK24323 ...
1 ....
2 ....
3 ....
I'm newbie about batch and programming, could someone kindly help me?
Thanks!
David.
I have a txt file like this:
0 ABCDE67890 12345FGHIJ ...
1 12SAG3J590 FGDG45346Y ..
2 GASDFSD230 ...
3 ....
0 HFJSHSDK24323 ...
1 ....
2 ....
3 ....
I would create a batch file to search and remove all "0 " (zero+space) only at the begin of lines, preserving every other occurrences. The output should be like this:
ABCDE67890 12345FGHIJ ...
1 12SAG3J590 FGDG45346Y ..
2 GASDFSD230 ...
3 ....
HFJSHSDK24323 ...
1 ....
2 ....
3 ....
I'm newbie about batch and programming, could someone kindly help me?
Thanks!
David.
Re: Remove character at begin of line
Here is a robust and quick solution:
This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
Code: Select all
@echo off
type "file.txt" | repl "^0 " "" >"newfile.txt"
This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
Re: Remove character at begin of line
Code: Select all
@(for /f "useback tokens=* delims=0 " %%i in ("example.txt") do @echo.%%i)>"new.txt"
Re: Remove character at begin of line
With the data as shown it will work, but it would be very useful to future readers to
state that it will not work as shown with multiple spaces or multiple zeros at the start of the line.
state that it will not work as shown with multiple spaces or multiple zeros at the start of the line.
Re: Remove character at begin of line
foxidrive wrote:With the data as shown it will work, but it would be very useful to future readers to
state that it will not work as shown with multiple spaces or multiple zeros at the start of the line.
This code does not affect the rest of zeros and spaces, and will also work with poison characters and with exclamation marks:
Code: Select all
@(for /f "usebackq delims=" %%i in ("example.txt") do @set x=%%i& cmd /v:on /c echo.!x!|>nul findstr /bc:"0 "&&cmd /v:on /c call echo.!x:*0 =!|| echo.%%i)>"new.txt"
Re: Remove character at begin of line
This will still nuke any leading spaces and any numbers or text before the first "0 " on all the other lines, true?
Yury wrote:.Code: Select all
&cmd /v:on /c call echo.!x:*0 =!
Re: Remove character at begin of line
foxidrive wrote:This will still nuke any leading spaces and any numbers or text before the first "0 " on all the other lines, true?Yury wrote:.Code: Select all
&cmd /v:on /c call echo.!x:*0 =!
foxidrive, this will nuke only the text before the first "0 " and the first "0 ", but with
Code: Select all
findstr /bc:"0 "
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Remove character at begin of line
vbscript
Code: Select all
Set objFSO=CreateObject("Scripting.FileSystemObject")
inputFile = WScript.Arguments(0)
Set objFile = objFSO.OpenTextFile(inputFile)
Do Until objFile.AtEndOfStream
strNextLine = objFile.ReadLine
Do While Mid(strNextLine,1,1) = "0" Or Mid(strNextLine,1,1) = " "
strNextLine = Mid(strNextLine,2,Len(strNextLine) )
Loop
WScript.Echo strNextLine
Loop
Code: Select all
cscript //nologo removeZeroesSpaces.vbs file
Re: Remove character at begin of line
Yury wrote:foxidrive, this will nuke only the text before the first "0 " and the first "0 ", but withonly the first "0 " are deleted.Code: Select all
findstr /bc:"0 "
Sorry Yury, I didn't pay close enough attention.
Re: Remove character at begin of line
Hello guys,
all solutions works great for my needs!
Thanks very much to all for your help and kindness.
Regards.
David.
all solutions works great for my needs!
Thanks very much to all for your help and kindness.
Regards.
David.