Write txt file

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
carkons
Newbie
Posts: 16
Joined: Wed Jul 27, 2011 12:33 am

Write txt file

Post by carkons » Thu Aug 20, 2015 12:00 am

hi everybody i have a small problem and i didnt resolve it..i try something but not worked.
i have a big txt file about (400.000 line)..it has many many ingredients for recipe


Image

i want to write every ingredients information(each recipe info separate with a blank line as you see a red colour) a different txt files.How can do this.Please help me

hagchr
Automation Wizard
Posts: 330
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Write txt file

Post by hagchr » Thu Aug 20, 2015 9:04 am

Hi, several ways you can do it, eg
- Loop through the source file line by line and create a new file when you hit the empty lines (slow and easy);
- Use Separate> and use the empty lines as delimiters and create the files (faster and half easy);
- Use RegEx> to extract each recipe and write to file (fastest? and slightly? difficult)

I am used to RegEx so am using that here, will take my spontaneous file with recipes (600,000 lines) and create the 50,000 files in around 1 minute. If you are interested I can post the Separate> version later.

Code: Select all

Let>SourceFile=C:\...\recipe.txt
Let>TargetPath=C:\yourpath...\

ReadFile>SourceFile,strFileContents
Let>tmp0=(?s)(?:\A|[^\s]).+?(?=(\r\n\s)|\Z)
RegEx>tmp0,strFileContents,0,m,nm,0
Let>WLN_NOCRLF=1
    
Let>ct=0
While>ct<nm
    Add>ct,1
    Let>file=%TargetPath%%ct%.txt
    WriteLn>file,nWLNRes,m_%ct%
EndWhile

MDL>Ready

carkons
Newbie
Posts: 16
Joined: Wed Jul 27, 2011 12:33 am

Re: Write txt file

Post by carkons » Thu Aug 20, 2015 10:09 am

thank you very much for a faster solution.i will try it when i go home (at work now)..can you send me Separate> version when you are free..thanks again

hagchr
Automation Wizard
Posts: 330
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Write txt file

Post by hagchr » Thu Aug 20, 2015 3:12 pm

Hi again,

Here a version using Separate>. For comparison, my example in the previous post would take 2.5 min, ie is slightly slower but is much easier to follow. Important that you have only one space between the sections or you will get some empty files (the RegEx version ignores the empty lines so not relevant there). Also, you read the whole file into a variable so you need enough memory for that. Otherwise you have to the read/write the files line by line which would be even slower.

Code: Select all

Let>SourceFile=C:\...\recipe.txt
Let>TargetPath=C:\yourpath...\

ReadFile>SourceFile,strFileContents
Let>tmp0=%CRLF%%CRLF%
Separate>strFileContents,tmp0,m

Let>WLN_NOCRLF=1
Let>ct=0
While>ct<m_COUNT
    Add>ct,1
    Let>file=%TargetPath%%ct%.txt
    WriteLn>file,nWLNRes,m_%ct%
EndWhile

MDL>Ready

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts