Computing.Net > Forums > Programming > Split txt file after a given word

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.

Split txt file after a given word

Reply to Message Icon

Name: Dawelian
Date: October 12, 2009 at 12:21:48 Pacific
OS: Windows Vista & UBUNTU
Subcategory: Batch
Comment:


Hello guys, i want to do a batch file to split a text file into multiple txt files every time there is a given word inside the txt.

for example:

(content of text.txt)
bla bla bla bla
CAR No. 1
bla bla bla bla
bla bla bla bla
CAR No. 2
bla bla bla bla
bla bla bla bla
bla bla bla bla
CAR No. 3

and i want to split it into 3 txt files, the first of them containing CAR No.1 and bla bla bla...
the second containing CAR No.2 and bla bla

and so on.

thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 12, 2009 at 14:00:54 Pacific
Reply:

@echo off & setLocal EnableDELAYedExpansion

if exist file?.txt del file?.txt

for /f "tokens=* delims= " %%a in (text.txt) do (
set str=%%a
echo !str! | find "CAR No" > nul
if not errorlevel 1 (
set dest=!str!
if defined dest set dest=!dest:~-1!
)
if defined dest echo !str!>> file!dest!.txt
)


=====================================
Helping others achieve escape felicity

M2


0

Response Number 2
Name: Billsahiker
Date: November 5, 2009 at 06:32:29 Pacific
Reply:

XmlMax will do this using an xpath expression. It will do it in a single pass using the unqiue file names option, but it does not run in batch mode. It is a GUI program. Get the free trial from xponentsoftware


0

Response Number 3
Name: Mechanix2Go
Date: November 6, 2009 at 04:36:28 Pacific
Reply:

It obviously wasn't urgent.


=====================================
Helping others achieve escape felicity

M2


0

Response Number 4
Name: Sen Hu
Date: November 9, 2009 at 08:29:40 Pacific
Reply:

# Script SplitFile.txt
var str file, str
var str content ; cat $file > $content
while ( { sen -c -r "^"+str+"&\n^" $content } > 0 )
do
    echo "\n\n\n\nNEXT SPLIT FOLLOWS.\n"
    stex -c -r "^"+str+"&\n^]" $content
done
echo "\n\n\n\nLAST SPLIT FOLLOWS.\n"
echo $content

Script is in biterscripting ( http://www.biterscripting.com ) . Save the script in file C:/Scripts/SplitFile.txt, call it using the command

script "C:/Scripts/SplitFile.txt" file(C:/text.txt") str("CAR No.")

Will show you splits of file C:/text.txt split at the string "Car No.". To create split files, just redirect the output of the stex command with > . Increment the file name after each split.



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Split txt file after a given word

batch to edit a txt file www.computing.net/answers/programming/batch-to-edit-a-txt-file/15327.html

batch rename files with a part of.. www.computing.net/answers/programming/batch-rename-files-with-a-part-of/16747.html

del text in txt file except dir www.computing.net/answers/programming/del-text-in-txt-file-except-dir/17223.html