Page 1 of 1
Remove character at begin of line
Posted: 30 Oct 2014 12:39
by DavideX
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.
Re: Remove character at begin of line
Posted: 31 Oct 2014 05:29
by foxidrive
Here is a robust and quick solution:
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.batPlace `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
Posted: 31 Oct 2014 06:07
by Yury
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
Posted: 31 Oct 2014 06:16
by foxidrive
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.
Re: Remove character at begin of line
Posted: 31 Oct 2014 07:39
by Yury
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
Posted: 31 Oct 2014 08:13
by foxidrive
This will still nuke any leading spaces and any numbers or text before the first "0 " on all the other lines, true?
Re: Remove character at begin of line
Posted: 31 Oct 2014 09:00
by Yury
foxidrive wrote:This will still nuke any leading spaces and any numbers or text before the first "0 " on all the other lines, true?
foxidrive, this will nuke only the text before the first "0 " and the first "0 ", but with
only the first "0 " are deleted.
Re: Remove character at begin of line
Posted: 31 Oct 2014 20:46
by ghostmachine4
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
Posted: 31 Oct 2014 22:04
by foxidrive
Yury wrote:foxidrive, this will nuke only the text before the first "0 " and the first "0 ", but with
only the first "0 " are deleted.
Sorry Yury, I didn't pay close enough attention.
Re: Remove character at begin of line
Posted: 02 Nov 2014 12:23
by DavideX
Hello guys,
all solutions works great for my needs!
Thanks very much to all for your help and kindness.
Regards.
David.