next up previous contents index
Next: merging files and adding Up: Examples Previous: table2sus


text2sus

Given a simple text file, a table can be constructed by giving simply labels or labels followed by regular expressions. The following input file is scanned for instance, running_time, value, and something more complex: the last word of a line with more at the beginning. This word gets the label complex. Additionally, we want the values of time as hours and minutes.

Note that the order of the labels does not matter and it is not necessary to have all lables in each of the records. An exception is the first label: It defines when to start a new record. (Alternatively one could use the --next switch.)

Command:
text2sus instance running_time value 'complex=^more.* (\w+)'
'time: (?P<hours>.*):(?P<minutes>.*)' < infile >
demofile2.sus

The reason for the ' ' in this command is that otherwise the shell gets confused by the * and the spaces.

infile

instance: steiner1.stp
time: 11:23
blablabla
more complicated, we want the last word in this line! 23
value: 123
running_time: 123

instance: steiner2.stp
more 44
time: 11:44
value: 312
running_time: 323

instance: steiner3.stp
time: 11:55
more 45
value: 32
running_time: 532

instance: steiner4.stp
time: 12:04 
more 46
value: 44
running_time: 954

demofile2.sus

{'labels': ['instance', 'running_time', 'value', 'complex', 'hours',
   'minutes'],
 'table': [{'running_time': '123', 'value': '123', 'minutes': '23',
   'complex': '23', 'instance': 'steiner1', 'hours': '11'},
 {'running_time': '323', 'value': '312', 'minutes': '44', 'complex':
   '44', 'instance': 'steiner2', 'hours': '11'},
 {'running_time': '532', 'value': '32', 'minutes': '55', 'complex':
   '45', 'instance': 'steiner3', 'hours': '11'},
 {'running_time': '954', 'value': '44', 'minutes': '04 ', 'complex':
   '46', 'instance': 'steiner4', 'hours': '12'}]}



Susan Hert
2002-08-29