how to parse a text file based on a field

August 2, 2012 at 11:26:48
Specs: Windows
Sorry for the newbie question.
I need to split my text file into separate files based on the second delimited field. I would prefer to do this with a batch script.
Sample data:

abc|123abc|test
agh|123def|test
ade|123abc|test

Thanks


See More: how to parse a text file based on a field

Report •

#1
August 2, 2012 at 12:24:15
:: ===== script starts here ===============
::
:: cm.bat 2012-08-03 2:18:13.32
@echo off > newfile & setLocal enableDELAYedeXpansioN
@echo off > newfile2 & @echo off > newfile3

for /f "tokens=1-3 delims=|" %%a in (thefile) do (
>> newfile echo %%a
>> newfile2 echo %%b
>> newfile3 echo %%c
)
::====== script ends here =================


====================================
Life is too important to be taken seriously.

M2


Report •

#2
August 2, 2012 at 12:34:48
Thank you for the quick reply! I may have miss stated what i wanted for an output. Based on the sample file I would need 2 separate files created, grouped by the second field.

file1
abc|123abc|test
ade|123abc|test

file2
agh|123def|test


Report •

#3
August 2, 2012 at 14:02:51
for /f "tokens=1,2* delims=|" %%a in (thefile) do @>> %%b echo %%a|%%b|%%c

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 2, 2012 at 14:21:55
I can't seem to get it working correctly. It is creating 3 blank files newfile, newfile2 and newfile3.

I replaced the "for" line.


Report •

#5
August 2, 2012 at 14:28:41
I got the split to work. I needed to add a ^ before the |.

I would like to build on this and have the filename be the same as the input name and if it needs to split add some kind of unique identifier.


Report •

#6
August 2, 2012 at 14:32:49
Forgot to add in the escape characters, but I fixed that. Mechanix2Go's offering and mine are two separate scripts, so don't replace just the "for" line. Replace everything.
set file=thefile
for /f "tokens=1,2* delims=|" %%a in (%file%) do @>> "%file%.%%b" echo %%a^|%%b^|%%c

EDIT: Due to the way batch files operate, this is probably the closest you're going to get in terms of the output file names.

How To Ask Questions The Smart Way


Report •

#7
August 2, 2012 at 16:54:51
Would you be able to recommend another option maybe VB?

Report •

#8
August 2, 2012 at 21:07:27
I still don't know what the requirement is.


====================================
Life is too important to be taken seriously.

M2


Report •

#9
August 6, 2012 at 06:58:45
I figured out a way to use the batch script..Thanks for your help

Report •

Ask Question