Text splitter based on text criteria

Split text file:

I have a text file with hundreds of lines, each line starts with either "N" or "I"

The help I would need is the file should copy from N(New line) and lines with "I" needs to be in single file; from the below example N- 00000 is one file N- 00001 and N- 00002 is another file

example:

N- 00000
I-0000
I-0001
I-0002
I-0003
N- 00001
I-0000
I-0001
I-0002
I-0003
N- 00002
I-0000
I-0001
I-0002
I-0003

Please help either with vbs or windows command line or regex or vba
NirvanalearnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
My vb is not there, but why not go live by line, check if the first charcter is N, if it is the prior line is termed, and this is a new one.

I.e.
$line += ...

...

The logic is fairly straightforward.
0
arnoldCommented:
The following is an example that combines all

https://blogs.technet.microsoft.com/heyscriptingguy/2007/08/15/how-can-i-combine-lines-in-a-text-file/

Adding a test on first charcter .........you can decide the prior ended, and this is the beginning.
0
Jose Gabriel Ortega CastroCEOCommented:
Hi Nirvana, I'm not sure I understand what you want.
If the file starts with N ( it's a new file) and should comply XXXXX (the number of lines within that file) into another file with what name?

Example: N- 000001
Create a file copying the 1 line into this new file? (what would be the name?)

Or:
N- 00000
I-0000
I-0001
I-0002
I-0003

Create a new file and create ( 0 lines, l-0000)
One file with (1) line
One file with (2) lines?
And so on?

Or how should we understand what you pretend to do? Can you do a more detailed example?, including example file names?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

NirvanalearnerAuthor Commented:
Hi Jose Gabriel Ortega C

Sorry for the late reply and not detailing the question. The text file is generated from JDE ( an application) which consists of below pattern.

I need to copy all lines from N to next N meaning the text file should get a split with every N "- 00000" corresponding zeros after are not consistent (it could be any four digit number like 1234, 3507 etc) so It just it just have pick N and lines starting with "I" in one file and "N" after a I should be new file; Hope I am clear

attached are the example files

Tex file 1 need to named as "N- 00000"

N- 00000
I-0000
I-0001
I-0002
I-0003

Tex file 2 need to named as as "N- 00001"
N- 00001
I-0000
I-0001
I-0002
I-0003

Tex file 3 need to named as as "N- 00002"
N- 00002
I-0000
I-0001
I-0002
I-0003
master_file.txt
N-0000-current-date-and-time.txt
N-0001-current-date-and-time.txt
N-0002-current-date-and-time.txt
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may give this a try...
In the attached, click the button called "Split Text File" to run the code.
Sub SplitTextFile()
Dim swb As Workbook, dwb As Workbook
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long, i As Long
Dim rng As Range, tRng As Range
Dim TextFile As String, NewTextFileName As String, SaveAsPath As String

Application.ScreenUpdating = False

With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select A Text File To Split"
    .AllowMultiSelect = False
    .Filters.Clear
    .Filters.Add "Text Files", "*.txt"
    If .Show = -1 Then
        TextFile = .SelectedItems(1)
    Else
        MsgBox "You din't select any Text File!", vbExclamation
        Exit Sub
    End If
End With
Workbooks.OpenText (TextFile)
Set swb = ActiveWorkbook
Set sws = swb.Sheets(1)
SaveAsPath = swb.Path & "\"

lr = sws.Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 1 Step -1
    If Left(sws.Cells(i, 1), 1) = "N" Then
        Rows(i).Insert
    End If
Next i
Set dwb = Workbooks.Add
Set dws = dwb.Sheets(1)

lr = sws.Cells(Rows.Count, 1).End(xlUp).Row
On Error Resume Next
Set tRng = sws.Range("A1:A" & lr).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0

If Not tRng Is Nothing Then
    For Each rng In sws.Range("A1:A" & lr).SpecialCells(xlCellTypeConstants, 3).Areas
        dws.Cells.Clear
        NewTextFileName = rng.Cells(1).Value & "-" & Format(Now, "mm-dd-yy hhmmss")
        NewTextFileName = SaveAsPath & NewTextFileName & ".txt"
        rng.Copy dws.Range("A1")
        dwb.SaveAs NewTextFileName, xlText
    Next rng
End If
swb.Close False
dwb.Close False
Application.ScreenUpdating = True
MsgBox "Task completed.", vbInformation
End Sub

Open in new window

Split-Text-File.xlsm
1
NirvanalearnerAuthor Commented:
Thank you very much sir. Only one ask can we have the file name as "=MID(A1,3,4)". as sometime I will have "|" pipe in the line so it would not get saved
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
In that case, why not replace line#44 with the following one?

NewTextFileName = Replace(rng.Cells(1).Value, "|", "-") & "-" & Format(Now, "mm-dd-yy hhmmss")

Open in new window

0
NirvanalearnerAuthor Commented:
Thank you very much sir but I need the send to 6 th character. I cannot do modifications to text file as it is an input to other files.

for example N- 00000 this would be like N|0000 ******** (some characters) and I need the file name as 0000 (next four numbers after pipe Now, "mm-dd-yy hhmmss

sorry for trouble
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Then try this...
NewTextFileName = Mid(Rng.Cells(1).Value, 3, 4) & "-" & Format(Now, "mm-dd-yy hhmmss")

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NirvanalearnerAuthor Commented:
Works like Grand. You should be in VBA coaching sir. Thanks a lot
1
NirvanalearnerAuthor Commented:
thank you thank you
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome Nirvana! Glad it worked as desired.
Thanks for the feedback and compliment. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.