1

我有一个格式如下的日志文件:

timestamp=123;
data1=value1;
data2=value2;
                       <-- empty line
timestamp=456;
data3=value3;
data4=value4;

我可以使用哪些 unix 命令将其转换为这种格式:

timestamp=123,data1=value1,data2=value2
timestamp=456,data3=value3,data4=value4
4

2 回答 2

1

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

sed -r ':a;$!N;s/;\n/,/;ta;s/,(\n)/\1/;$s/;//;P;D' file

或这个:

sed -r ':a;$!N;s/;\n(timestamp)/\n\1/;s/;\n/,/;ta;s/,(\n)/\1/;$s/;//;P;D' file
于 2013-08-23T07:35:54.153 回答
1

awk 呢?

#!/bin/bash

awk '
BEGIN {
    FS = ";"; # $1 will contain everything but the semicolon 
    first_item = 1;
} {
    if ($1 == "") { # handle empty line
            printf "\n";
            first_item = 1;
            next;
    }

    if (first_item != 1) { # avoid comma at the end of the line
            printf ",";
    } else {
            first_item = 0;
    }

    printf "%s", $1; # print the item
} END {
    printf "\n";
}'

如果输入保存在 input.txt 中并且上述脚本命名为 to_csv.sh,则以下命令将产生所需的输出:

./to_csv.sh < input.txt
于 2013-08-23T07:07:58.457 回答