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
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
Write txt file
Moderators: Dorian (MJT support), JRL
Re: Write txt file
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.
- 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
Re: Write txt file
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
Re: Write txt file
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.
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