Remove character at begin of line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DavideX
Posts: 2
Joined: 30 Oct 2014 12:28

Remove character at begin of line

#1 Post by DavideX » 30 Oct 2014 12:39

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove character at begin of line

#2 Post by foxidrive » 31 Oct 2014 05:29

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.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Remove character at begin of line

#3 Post by Yury » 31 Oct 2014 06:07

Code: Select all

@(for /f "useback tokens=* delims=0 " %%i in ("example.txt") do @echo.%%i)>"new.txt"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove character at begin of line

#4 Post by foxidrive » 31 Oct 2014 06:16

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.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Remove character at begin of line

#5 Post by Yury » 31 Oct 2014 07:39

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"
.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove character at begin of line

#6 Post by foxidrive » 31 Oct 2014 08:13

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 =!

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Remove character at begin of line

#7 Post by Yury » 31 Oct 2014 09:00

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 "
only the first "0 " are deleted.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Remove character at begin of line

#8 Post by ghostmachine4 » 31 Oct 2014 20:46

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove character at begin of line

#9 Post by foxidrive » 31 Oct 2014 22:04

Yury wrote:foxidrive, this will nuke only the text before the first "0 " and the first "0 ", but with

Code: Select all

findstr /bc:"0 "
only the first "0 " are deleted.


Sorry Yury, I didn't pay close enough attention.

DavideX
Posts: 2
Joined: 30 Oct 2014 12:28

Re: Remove character at begin of line

#10 Post by DavideX » 02 Nov 2014 12:23

Hello guys,
all solutions works great for my needs!

Thanks very much to all for your help and kindness.

Regards.

David.

Post Reply