Since you tagged this R, I will provide an R answer. Here is a base R method first, with a data.table method down below.
## read the data into R
df <- read.table("input.txt")
## split the data frame by the first column
s <- split(df, df[[1L]])
## write each table in 's' to file 'out*.txt'
Map(write.table, x = s, file = sprintf("out%s.txt", seq_along(s)))
Now you should have three new files "out1.txt", "out2.txt", and "out3.txt" based on your example data.
Alternatively, we can speed this up with the data.table package.
## read the data
dt <- fread("input.txt")
## write each chunk of 'dt' to file 'out*.txt' by group
dt[, write.table(cbind(V1 = .GRP, .SD), sprintf("out%s.txt", .BY)), by = V1]
Obviously this makes some assumptions about column names, but it is easy to change those values accordingly.
Asked in February 2016Viewed 3,859 timesVoted 11Answered 2 times