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
NirvanamanagerAsked:
Who is Participating?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
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
 
NirvanamanagerAuthor 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
 
NirvanamanagerAuthor 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
 
NirvanamanagerAuthor 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
 
NirvanamanagerAuthor Commented:
Works like Grand. You should be in VBA coaching sir. Thanks a lot
1
 
NirvanamanagerAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.