Computing.Net > Forums > Programming > Batch extraction to text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch extraction to text file

Reply to Message Icon

Name: banshie
Date: April 20, 2009 at 09:46:39 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hello there,

Anyone can help me to get this working please. I need to extract from a text file to a new text file a portion of code based on a start and end pattern.

Start pattern it's always on two lines as this:

<li type='disc'><nobr>
STARTPATTERN

... and I would like to extract from the first line of this text (<li type....) until a second pattern like this one:

<li type='disc'><nobr>
ENDPATTERN

...but in this case without taking nothing of these text, jut until the previous line.

Yes, I know it's a bit strange, but I've googled for more than an hour and found no app or command line tool able to do this.

I will be very grateful if anyone can help me with this.

Sweet greets,

banshie



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 20, 2009 at 14:08:08 Pacific
Reply:

Why does this seem so eerily familiar?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: banshie
Date: April 20, 2009 at 15:39:48 Pacific
Reply:

I had to search "eerily" because my English doesn't go so deep...

eerili: def. Inspiring inexplicable fear, dread, or uneasiness; strange and frightening.

Well... I really though anyone could help me ... firstly I tried for myself finding a solution, tried to search for a standalone product able to do this and unexpectedly there's no such a program, commercial or not, capable of extract text between two diferent delimiters. I tried first for command line but finally wouldn't have matter if it was gui-based ... but not luck.

Then I tried to search for batch programming and almost all interesting and near what i was searching searches guided me to cumputing.net forums. Before posting I tried to search previous posts and related topics, and although there was no solution for my search I could found out a very helpful member community. I get really surprised there wear lots of question answered when everybody knows what happens in this sort of help forums ... lots of uneducated "questioners" and a few "helpers".

Sorry if I didn't follow any step, I'm not here for disturbing, I only thought I could get a little help here as I love to do when someone needs my help on something i can share my knowledge.

Sweet greets,

banshie


0

Response Number 3
Name: Mechanix2Go
Date: April 20, 2009 at 17:25:25 Pacific
Reply:

Works on myfile; outputs to newfile.

=================================
@echo off > newfile & setLocal EnableDelayedExpansion

set /a N=0

for /f "tokens=* delims=" %%a in (myfile) do (
set /a N+=1
set curr=%%a
if "!curr!" equ "STARTPATTERN" if "!prev!" equ "<li type='disc'><nobr>" (
set /a pre=!N!
)
set prev=!curr!
)

set /a N=0

for /f "tokens=* delims=" %%a in (myfile) do (
set /a N+=1
set curr=%%a
if "!curr!" equ "ENDPATTERN" if "!prev!" equ "<li type='disc'><nobr>" (
set /a post=!N!-1
)
set prev=!curr!
)

set /a N=0

for /f "tokens=* delims=" %%a in (myfile) do (
set /a N+=1
set str=%%a
if !N! gtr !pre! if !N! lss !post! (echo !str! >> newfile)
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: banshie
Date: April 21, 2009 at 15:12:18 Pacific
Reply:

Many thanks ... you're my hero ;)

I've made a few changes and maybe this way this great script by Mechanix2Go can help more people than only me... this is the new code:

Script Purpose: Extract a portion of text between two delimiters from a text file and output it to a new file. Valid for any text file, html included.

--------------------------------------------------------------
@echo off > newfile.txt & setLocal EnableDelayedExpansion

set /a N=0
set offsetup=0
set offsetdown=0

for /f "tokens=* delims=" %%a in (originalfile.txt) do (
set /a N+=1
set curr=%%a
if "!curr!" equ "START_PATTERN" (
set /a pre=!N!-1+!offsetup!
)
set prev=!curr!
)

set /a N=0

for /f "tokens=* delims=" %%a in (originalfile.txt) do (
set /a N+=1
set curr=%%a
if "!curr!" equ "END_PATTERN" (
set /a post=!N!+1+!offsetdown!
)
set prev=!curr!
)

set /a N=0

for /f "tokens=* delims=" %%a in (originalfile.txt) do (
set /a N+=1
set str=%%a
if !N! gtr !pre! if !N! lss !post! (echo !str! >> newfile.txt)
--------------------------------------------------------------

You can customize these fields to fix your needs.

originalfile.txt: The file with the text to be extracted.
newfile.txt: The created file with the extracted lines.
START_PATTERN: Pattern FROM which NEXT lines will be extracted.
END_PATTERN: Pattern UNTIL which lines BEFORE will be extracted.
set offsetup=0 Offset lines at START of extracted text. Zero means the line with START_PATTERN will be included, -1 means previous line and +1 means extract will start from next line. You can give it any value (logical ;).
set offsetdown=0 Offset lines at the END of extracted text. Zero means the line with END_PATTERN will be the last included, -1 means until previous line and +1 means extract will end on next line. You can give it any value (logical again;).

Well, hope this can help anyone around, credits to Mechanix2Go!

Sweet greets to everyone!


//banshie


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch extraction to text file

printing variable to text file www.computing.net/answers/programming/printing-variable-to-text-file/14677.html

Windows Batch + C++ to copy files? www.computing.net/answers/programming/windows-batch-c-to-copy-files/6254.html

BATCH reading structured Text File www.computing.net/answers/programming/batch-reading-structured-text-file/14544.html