how to delete records from a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cirugio
Posts: 2
Joined: 03 Mar 2015 15:27

how to delete records from a text file

#1 Post by cirugio » 04 Mar 2015 14:42

Hoping someone can assist. I am trying to write a DOS batch file which will delete all rows where in the 7th position the values equals '0007'. I found the following code while researching on the internet but it does not quite work. When I run it, nothing gets created. Can someone help me with the code? Thanks for your time in advance.

DOS code that is not working:

Code: Select all

set InFile=c:\time_deposits.txt
set InFile=C:\input.txt
set OutFile=c:\output.txt
if exist "%OutFile%" del "%OutFile%"
for /f "skip=0 delims=" %%a in ('type "%InFile%"') do (
   set Line=%%a
   set SkipLine=0
   set Currency=!Line:~12,4!
   if /i "!Currency!"=="0007" set SkipLine=0
   ::if /i "!Currency!"=="0075" set SkipLine=0
   if !SkipLine!==0 echo.!Line!
) >>outfile


sample of Input file records:
20150402 0007CD 003178 0.0000000000000
20150402 0007CD 005555 0.0000000000000
20150402 0013CD 006565 0.0000000000000
20150402 0017CD 004428 0.0000000000000
Last edited by cirugio on 04 Mar 2015 16:15, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how to delete records from a text file

#2 Post by Squashman » 04 Mar 2015 15:34

Isn't that the 10th position?
set Currency=!Line:~9,4!

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how to delete records from a text file

#3 Post by Squashman » 04 Mar 2015 15:47

This seems like it would be easier. But maybe there is some caveat to doing it this way.

Code: Select all

C:\BatchFiles\FINDSTR>findstr /R /V /C:"^.........0007" cirugo.txt
20150402 0013CD 006565 0.0000000000000
20150402 0017CD 004428 0.0000000000000

cirugio
Posts: 2
Joined: 03 Mar 2015 15:27

Re: how to delete records from a text file

#4 Post by cirugio » 04 Mar 2015 16:09

Actually, it is in the 12 position. For some reason, when I cut and paste the sample records it removed the 3 spaces between the date and the value i am searching on (see new sample below). I also can't use the FINSTR because the first 8 character is a date and it changes everyday. I have provided a new sample of data below along with the code again. The code creates an empty file with no records. I should at least get 2 records listed in it.


sample data
20150402xxx0007CDxxx003178x0.0000000000000R
20150402xxx0007CLxxx397370x0.0060000000000U
20150402xxx0067CLxxx448742x0.0060000000000B
20150402xxx0077CLxxx460727x0.0060000000000B


code trying to get to work :

Code: Select all

set InFile=c:\input.txt
set OutFile=c:\output.txt
if exist "%OutFile%" del "%OutFile%"
for /f "delims=" %%a in ('type "%InFile%"') do (
   set Line=%%a
   set Branch=!Line:~12,4!
   if /i "!Branch!"=="0007" set SkipLine=1
   ::if /i "!Branch!"=="0075" set SkipLine=1
   if !SkipLine!==0 echo.!Line!
) >>outfile

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how to delete records from a text file

#5 Post by Squashman » 04 Mar 2015 17:31

Substring is an OFFSET. Not the starting position. Thought you might have noticed that when I said starting position ten but used 9 for the offset.

What does date have anything to do with my FINDSTR code. I was telling it to find any 9 characters and then 0007 at the beginning of a line.
So again, tell my why you can't use FINDSTR.

Code: Select all

findstr /R /V /C:"^...........0007" cirugo.txt


And please get the hint on using CODE tags.

Post Reply