I would like to convert data.txt into scheme list using sed into the following format:
-Each every line with same starting number will be parsed and combined like so:
data.txt
1,{},344.233
1,{2},344.197
2,{16},290.281
2,{18},289.093
3,{1},220.896
foo.scm
(define v1 '(1 (() 344.233) ((2) 344.197))) ;; this is for first two lines starting with 1
(define v2 '(2 ((16) 290.281) ((18) 289.093))) ;; ... 2
(define v3 '(3 (() 237.558))) ;; ... 3
I know nothing about scheme, so I’d probably do this in awk rather than sed.
It could certainly be written more tightly, but this gets the job done.
UPDATE: (per comments)
What’s going on here?
In the new block we’re adding, test to see whether the second field ends in a
}. If it doesn’t, we’ll loop until it does. For each run of the loop, we’ll remove a comma before the}, replacing it with a space.Sometimes, brute-force works. 😛