csaam
csaam

Reputation: 165

How do I convert the following log file to CSV type file?

I have a log file formatted like this:

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

What unix commands can I use to convert it to this format:

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

Upvotes: 1

Views: 235

Answers (2)

potong
potong

Reputation: 58361

This might work for you (GNU sed):

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

or this:

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

Upvotes: 1

wooghie
wooghie

Reputation: 437

How about 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";
}'

If the input is saved in input.txt and the above script is named to_csv.sh, the following command will produce the desired output:

./to_csv.sh < input.txt

Upvotes: 1

Related Questions