Split text file
Leave feedback
GroupDocs.Merger allows to split text file into several resultant text files. The default behavior is to split each line into separate file.
Here are the steps on how to split text file as described:
- Initialize TextSplitOptions class with output files path format, desired TextSplitMode and line numbers;
- Instantiate Merger object with source document path or InputStream;
- Call split method and pass TextSplitOptions object to itfor saving resultant text files.
The following code sample demonstrates how to split text file to two one-page documents with 3rd, 6th lines from source file:
with gm.Merger("c:/sample.txt") as merger:
output_path = "c:/output/text_{0}.{1}"
split_options = gm.domain.options.TextSplitOptions(output_path, [3, 6])
merger.split(split_options)
This code snippet will produce:
Text file | Line numbers |
---|---|
line_0 | 3 |
line_1 | 6 |
The following code sample demonstrates how to split text file to several multi-line files starting from 3rd and ending at 6th line numbers:
with gm.Merger("c:/sample.txt") as merger:
output_path = "c:/output/text_{0}.{1}"
split_mode = groupdocs.merger.TextSplitMode.INTERVAL;
split_options = gm.domain.options.TextSplitOptions(output_path, split_mode, [3, 6])
merger.split(split_options)
This code snippet will produce:
Text file | Line numbers |
---|---|
text_0 | 1, 2 |
text_1 | 3, 4, 5 |
text_2 | 6 |
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.