extract element from text file in dos

March 18, 2011 at 14:56:33
Specs: Windows Vista
have a text file as follows
"U","0","7551","Y","HELLO","1"
"U","0","7552","Y","HELLO","1"
"U","0","7553","Y","HELLO","1"

and I need to extract the third element "755X" and send to another file so it looks like
7551
7552
7553
can this be done uning DOS commands?


See More: extract element from text file in dos

Report •

#1
March 18, 2011 at 18:26:36
Here's a Ruby for Windowsone-liner

C:\work\> ruby -i.bak -F"," -ane 'puts $F[2].gsub!(/\"/,"")' file

C:\work\> type file
7551
7552
7553



Report •

#2
March 18, 2011 at 20:03:21
Windows batch:

for /f "usebackq tokens=3 delims=," %%a in ("thefile.txt") do >> "newfile.txt" echo %%~a


Report •

#3
March 18, 2011 at 22:23:25
@Judago
 for /f "usebackq tokens=3 delims=," %%a in ("thefile.txt") do >> "newfile.txt" echo %%~a


what if there are line like these:

"U", "0", "7552","Y","HELLO","1"


Report •

Related Solutions

#4
March 18, 2011 at 22:52:20
@kurumi

It's a little longer, but this could be used:

for /f "usebackq tokens=3 delims=," %%a in ("thefile.txt") do (
    for /f %%b in ("%%a") do >> "newfile.txt" echo %%~b
)


Report •

#5
March 19, 2011 at 11:25:34
Thank you for the prompt replys they all work fine. My files are longer than the example but once I have the script I can amend it to the longer files.

Very much appreciate the prompt reply.

Thank's


Report •

Ask Question