2

我想使用 sed 将 data.txt 转换为方案列表,格式如下:

- 具有相同起始编号的每一行都将被解析和组合,如下所示:

数据.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
4

3 回答 3

3

我对方案一无所知,所以我可能会在 awk 而不是 sed 中执行此操作。

[ghoti@pc ~]$ cat data.txt 
1,{},344.233
1,{2},344.197
2,{16},290.281
2,{18},289.093
3,{1},220.896
[ghoti@pc ~]$ cat doit.awk 
#!/usr/bin/awk -f

BEGIN {
  FS=",";
  last1=1;
}

$1 != last1 {
  printf("(define v%.0f '(%.0f %s))\n", last1, last1, substr(sect,2));
  last1=$1; sect="";
}

{
  gsub(/[^0-9]/,"",$2);
  sect=sprintf("%s ((%s) %s)", sect, $2, $3);
}

END {
  printf("(define v%.0f '(%.0f %s))\n", last1, last1, substr(sect,2));
}

[ghoti@pc ~]$ ./doit.awk data.txt 
(define v1 '(1 (() 344.233) ((2) 344.197)))
(define v2 '(2 ((16) 290.281) ((18) 289.093)))
(define v3 '(3 ((1) 220.896)))
[ghoti@pc ~]$ 

它当然可以写得更紧密,但这可以完成工作。

更新:(根据评论)

[ghoti@pc ~]$ tail -1 data.txt 
3,{1,3,4},220.896
[ghoti@pc ~]$ diff -u doit.awk doitnew.awk 
--- doit.awk    2012-05-30 00:38:34.549680376 -0400
+++ doitnew.awk 2012-05-30 00:38:52.893810815 -0400
@@ -10,8 +10,15 @@
   last1=$1; sect="";
 }

+$2 !~ /}$/ {
+  while ($2 !~ /}$/) {
+    pos=match($0, /,[0-9,]+}/);
+    $0=substr($0, 0, pos-1) " " substr($0, pos+1);
+  }
+}
+
 {
-  gsub(/[^0-9]/,"",$2);
+  gsub(/[^0-9 ]/,"",$2);
   sect=sprintf("%s ((%s) %s)", sect, $2, $3);
 }

[ghoti@pc ~]$ ./doitnew.awk data.txt 
(define v1 '(1 (() 344.233) ((2) 344.197)))
(define v2 '(2 ((16) 290.281) ((18) 289.093)))
(define v3 '(3 ((1 3 4) 220.896)))
[ghoti@pc ~]$ 

这里发生了什么?

在我们添加的新块中,测试第二个字段是否以}. 如果没有,我们将循环直到它出现。对于循环的每次运行,我们将删除 之前的逗号},用空格替换它。

有时,蛮力有效。:-P

于 2012-05-30T03:32:21.813 回答
3

在球拍(又名计划)中:

#lang racket

;; parse a line (we will join them later)
(define (line-parse l)
  (match (regexp-match #px"([0-9]+),\\{([0-9,]*)\\},([0-9.]+)" l)
    [(list dc first-num bracket-nums rest)
     (list (string->number first-num)
           (match bracket-nums
             ["" empty]
             [else (map string->number
                        (regexp-split #px"," bracket-nums))])
           (string->number rest))]
    [else
     (error "unexpected line format in line: ~s\n" l)]))

;; join together lines that start with the same number
(define (join-lines lines)
  (cond [(empty? lines) empty]
        [else (join-lines-of-n (first (first lines)) 
                               lines
                               empty)]))

;; gather together lines starting with 'n':
(define (join-lines-of-n n lines accum)
  (cond [(empty? lines)
         (list (cons n (reverse accum)))]
        [(equal? (first (first lines)) n)
         (join-lines-of-n n (rest lines) (cons (rest (first lines))
                                               accum))]
        [else
         (cons (cons n (reverse accum))
               (join-lines lines))]))

(define (dress-up line)
  (format "~a\n" `(define ,(format "v~s" (first line))
                  ',line)))


(display 
 (apply 
  string-append
  (map dress-up 
       (join-lines
        (map line-parse
             (sequence->list (in-port read-line)))))))

将它保存为 rewrite.rkt,像这样运行它:

oiseau:/tmp clements> racket ./rewrite.rkt < foo.txt
(define v1 (quote (1 (() 344.233) ((2) 344.197))))
(define v2 (quote (2 ((16) 290.281) ((18) 289.093))))
(define v3 (quote (3 ((1) 220.896) ((4 5) 2387.278))))

...请注意,我在输入示例中添加了 {4,5} 行来测试您的扩展。

另外,请注意输出使用 (quote ...) 而不是 '(...)。这“应该可以正常工作”;也就是说,Scheme 阅读器为这两种形式生成相同的输出,并且生成的文件应该可以作为方案输入正常工作。

如果这是我的代码,我想我不会跳 (define v1 ...) 跳舞,而只是把它写成一大段数据,一个方案/球拍程序可以通过一次“读取”来吞食,但这是你的选择,不是我的。此外,您的规范中存在一些歧义:初始索引的唯一性;也就是说,您可能会“返回”到较早的行号。例如,当给定这个输入文件时,输出应该是什么:

3,{1},1.0
4,{1},1.0
3,{1},1.0

?

另外,请注意,我删除了所有测试用例以使其看起来更短/更漂亮:)。

编辑:哦!相反,以这种方式收集线条。它实际上会慢一点,但它读起来更好:

#lang racket

;; parse a line (we will join them later)
(define (line-parse l)
  (match (regexp-match #px"([0-9]+),\\{([0-9,]*)\\},([0-9.]+)" l)
    [(list dc first-num bracket-nums rest)
     (list (string->number first-num)
           (match bracket-nums
             ["" empty]
             [else (map string->number
                        (regexp-split #px"," bracket-nums))])
           (string->number rest))]
    [else
     (error "unexpected line format in line: ~s\n" l)]))

;; does the line start with the number k?
(define ((starts-with k) l) (equal? (first l) k))

;; join together lines starting with the same thing:
(define (join-lines lines)
  (for/list ([k (remove-duplicates (map first lines))])
    (cons k (map rest (filter (starts-with k) lines)))))

(define (dress-up line)
  (format "~a\n" `(define ,(format "v~s" (first line))
                  ',line)))


(display 
 (apply 
  string-append
  (map dress-up 
       (join-lines
        (map line-parse
             (sequence->list (in-port read-line)))))))
于 2012-05-30T05:22:03.660 回答
0

这可能对您有用(GNU sed):

 sed ':a;$!N;s/^\(\([^,])*\).*\)\n\2/\1/;ta;h;x;s/\n.*//;s/,{\([^}]*\)},\([^,]\+\)/ ((\1) \2)/g;s/,/ /g;s/^\([^ ]*\).*/(define v\1 '\''(&)) ;;...\1/p;x;D' file

解释:

  • 将类似的值减少到一行:a;$!N;s/^\(\([^,])*\).*\)\n\2/\1/;ta
  • 复制模式空间 (PS) 以保留空间 (HS)。h
  • 交换到 HSx
  • 砍掉上一行。s/\n.*//
  • 制定清单。s/,{\([^}]*\)},\([^,]\+\)/ ((\1) \2)/g
  • 用空格替换任何剩余,的 '。s/,/ /g
  • 用函数定义和注释包围列表并打印。s/^\([^ ]*\).*/(define v\1 '\''(&)) ;;...\1/p
  • 换回PS。x
  • 删除上一行并重复。D
于 2012-05-30T10:45:46.893 回答