I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.
#!/usr/bin/env python3
import collections
output = collections.OrderedDict()
defwith my_splitopen(lineinputfile, 'r') as infile:
for line in infile:
dat, tmp = line.strip().split(',')
if '_''_val' in tmp:
var key, strvalidxstr = tmp.split('_val')
val = int(strval)
else:
varidx = tmpint(idxstr)
val = 0else:
return dat, var, val
with open('infile.txt', 'r') as infile:
key = fortmp
line in infile:
dat, var, validx = my_split(line.strip())0
output.setdefault(varkey, ["", "", ""])[val][idx] = dat
with open('outfile.txt'outoutfile, 'w') as outfile:
for var in output:
v = output[var]
outfile.write('{} {}\n'.format(var, ' '.join(v)))
Update: modified according to comments