Split .txt format mixed table file in two table files

56 views Asked by At

In R programming language, is it possible to split a .txt file like this: mixed_data.txt ->

enter image description here

in two .txt files like: file1.txt

enter image description here

and file2.txt

enter image description here

1

There are 1 answers

0
Gregor Thomas On BEST ANSWER

Here's an attempt based on your example:

# read in the original file
x = readLines("example_mixed_dataset.txt")

# break it into two pieces based on the first occurrence of the word "Index"
# and write to two separate files
index = grep("Index", x)
writeLines(x[1:(index - 1)], "file1.txt")
writeLines(x[index:length(x)], "file2.txt")

This code is obviously untested because I can't test on the picture of a file you posted. If there are issues, please post the sample data as text, not as a picture, otherwise I can't debug.