# DOS Batch file to pull certain info from a text file



## dspencer5 (Apr 4, 2013)

Guys,

I'm having trouble getting a dos batch file to work correctly to pull only certain lines from a text document. I need it to read through a .txt file and copy the lines that have an O (oh) in the 14th field and a three character value in the 19th comma delimited field to a blank txt document. Example text from my file is below the one that should be copied to a different .txt file is the one that begins with "222222". I appreciate any help I can get with this.


"111111","02","111476","Customer1","xxS48","001535 32601","111210","101110","1740.00",".00","011513", "P",2013-03-28 13:20,"S","","","044LP","111476","XXX"

"222222","02","111089","Customer2","xx2584","00153 270702","111610","081110","540.10",".00","092712", "P",2013-03-27 10:33,"O","","","044LP","111089","XXX"


----------



## Squashman (Apr 4, 2003)

1)Define what you mean by 3 character value.
2)Are there more delimited fields after the 19th field?


----------



## Squashman (Apr 4, 2003)

This works but is painfully slow.

```
findstr /B /R /C:".*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,\"O\",.*,.*,.*,.*,\"...\"" test.txt
```
Output

```
C:\Users\Squashman\batch\findstr>findo.bat
"222222","02","111089","Customer2","xx2584","00153 270702","111610","081110","540.10",".00","092712", "P",2013-03-27 10:33,"O","","","044LP","111089","XXX"

C:\Users\Squashman\batch\findstr>
```


----------



## dspencer5 (Apr 4, 2013)

Thanks for the reply. The 3 character value is the users initials and is the last value in the line of text.


----------



## foxidrive (Oct 20, 2012)

Did Squashman's code above help you?


----------



## dspencer5 (Apr 4, 2013)

It gives me something to experiment with but as he stated it is very slow, considering that these files generally have over 1500 lines it will take an awful long time to get through it. I'm also looking for a way to output what it does find to another text file so the user can print or email it. Thanks for the help so far squashman


----------



## Squashman (Apr 4, 2003)

http://www.robvanderwoude.com/redirection.php


----------



## foxidrive (Oct 20, 2012)

Squashman's code took 12:20 to process 1500 lines with a match in every second line.
I'd challenge you to process it manually any quicker. 

Still, this uses the same technique and GNUsed and processes the same file in under one second.


```
@echo off
echo %time%
sed "/.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,\x22O\x22,.*,.*,.*,.*,\x22...\x22/"!d test.txt >file.txt
echo %time%
pause
```


----------



## foxidrive (Oct 20, 2012)

This works for your set of data too, if you are searching for a specific set of initials. Takes just a few seconds.


```
@echo off
for /f "delims=" %%a in (test.txt) do (
for /f "tokens=14,19 delims=," %%b in ("%%a") do (

if %%b=="O" if %%c=="XXX" >>file.txt echo.%%a
)
)
pause
```


----------



## Squashman (Apr 4, 2003)

Yeah I was thinking about using a fro loop last night but got too tired. Figured you would provide a SED solution as well. That is what I used back in my Unix days.


----------



## dspencer5 (Apr 4, 2013)

Thank you, Thank you, Thank you guys for the help. The last example worked the best for me, ended up removing the search on the initials and just used the 14th field with the O and it is super fast. Couldn't have figured it out with out you guys.


----------

