Split a text file based on a delimiter

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
akhiler
Posts: 10
Joined: 10 Jun 2022, 14:27

Split a text file based on a delimiter

Post by akhiler » 21 Apr 2024, 00:06

Let's say I have a text file as below with the delimiter literally SPLIT LINE HERE. How can I split each block of text into a different file or a different variable.

SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4

SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4

SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
Banaanae
Posts: 34
Joined: 19 Feb 2024, 04:08
Contact:

Re: Split a text file based on a delimiter

Post by Banaanae » 21 Apr 2024, 03:42

Try this, makes the SplitArray which can then be processed in a for-loop

Code: Select all

Text := "
(
SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4

SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4

SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
)"

SplitArray := StrSplit(Text, "SPLIT LINE HERE`n")
For index, value in SplitArray ; Loop through each element (accessed as value) in the SplitArray
    MsgBox(value)
Use this to read a text file, instead of hardcoding

Code: Select all

Text := FileRead("MyFile.txt") ; Instead of hardcoding
Note because there is a "SPLIT LINE HERE" at the start of the string the first array element will be blank
User avatar
kunkel321
Posts: 1293
Joined: 30 Nov 2015, 21:19

Re: Split a text file based on a delimiter

Post by kunkel321 » 21 Apr 2024, 10:43

Banaanae wrote:
21 Apr 2024, 03:42
Try this, makes the SplitArray
Building on Banaanae's...

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

Text := "
(
SPLIT LINE HERE
text line #1a
text line #2a
text line #3a
text line #4a

SPLIT LINE HERE
text line #1b
text line #2b
text line #3b
text line #4b

SPLIT LINE HERE
text line #1c
text line #2c
text line #3c
text line #4c
)"

SplitArray := StrSplit(Text, "SPLIT LINE HERE`n")

MsgBox SplitArray[2]
MsgBox SplitArray[3]
MsgBox SplitArray[4]
And another variation:

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

Text := "
(
SPLIT LINE HERE
text line #1a
text line #2a
text line #3a
text line #4a

SPLIT LINE HERE
text line #1b
text line #2b
text line #3b
text line #4b

SPLIT LINE HERE
text line #1c
text line #2c
text line #3c
text line #4c
)"

SplitArray := StrSplit(Text, "SPLIT LINE HERE`n")

loop SplitArray.length - 1 ; number of segments, minus one because of the blank one.
{
	segment := A_Index + 1 ; +1 to skip the blank one.
	FileAppend(SplitArray[segment], "NewFile-" A_Index) ; "FileAppend" creates a new file, if one doesn't already exist. 
}
This one will create three new files in the same folder as where the script is saved.
name := "ste(phen|ve) kunkel"
akhiler
Posts: 10
Joined: 10 Jun 2022, 14:27

Re: Split a text file based on a delimiter

Post by akhiler » 21 Apr 2024, 21:21

great replies. thank you @Banaanae and @kunkel321!
The StrSplit function was essential as well as the proper way to loop starting with the second item in the array.
Post Reply

Return to “Ask for Help (v2)”