将多行日志文件转换为CSV

时间:2013-04-11 18:39:08

标签: linux shell csv sed awk

我的文件如下:

----------------------------------------
#Timestamp: 4/11/2013 12:00:48 AM
#Title: MDS error
#Category: Errors

----------------------------------------
#Timestamp: 4/11/2013 12:03:27 AM
#Title: MDS error
#Category: Errors

----------------------------------------
#Timestamp: 4/11/2013 12:05:39 AM
#Title: MDS error
#Category: Errors

----------------------------------------

我需要将其转换为CSV文件,如下所示:

4/11/2013 12:00:48 AM,MDS error,Errors
4/11/2013 12:03:27 AM,MDS error,Errors
4/11/2013 12:05:39 AM,MDS error,Errors

需要做一些命令行(awk / sed)吗?因为我有一系列像这样的文件需要转换成CSV。

7 个答案:

答案 0 :(得分:4)

awk -F: '/^#Timestamp/{line=$2","}/^#Title/{line=line""$2}/^#Category/{print line","$2;}' your_file

测试:

> cat temp
----------------------------------------
#Timestamp: 4/11/2013 12:00:48 AM
#Title: MDS error
#Category: Errors

----------------------------------------
#Timestamp: 4/11/2013 12:03:27 AM
#Title: MDS error
#Category: Errors

----------------------------------------
#Timestamp: 4/11/2013 12:05:39 AM
#Title: MDS error
#Category: Errors

----------------------------------------
> awk -F: '/^#Timestamp/{line=$2","}/^#Title/{line=line""$2}/^#Category/{print line","$2;}' temp
 4/11/2013 12, MDS error, Errors
 4/11/2013 12, MDS error, Errors
 4/11/2013 12, MDS error, Errors

如果对OP的确定,则是一个较短的解决方案:

awk -F: '/^#/{line=line","$2}/^-/{print substr(line,3);line="";}' your_file

答案 1 :(得分:1)

#!/bin/bash

while true; do
    read             || break
    read _ timestamp || break
    read _ title     || break
    read _ category  || break
    read             || break

    printf '%s,%s,%s\n' "$timestamp" "$title" "$category"
done < logfile.txt

答案 2 :(得分:1)

这可能适合你(GNU sed):

sed '/^#Timestamp:/{N;N;y/\n/,/;s/#[^ ]* //gp};d' file

答案 3 :(得分:0)

$ awk -F": " '/^#T/{printf "%s,",$2}/^#C/{printf "%s\n",$2}' file
4/11/2013 12:00:48 AM,MDS error,Errors
4/11/2013 12:03:27 AM,MDS error,Errors
4/11/2013 12:05:39 AM,MDS error,Errors

答案 4 :(得分:0)

假设每条记录只包含三行,您可以放弃清理输入并将其“粘贴”在一起:

<infile sed '/^---/d; /^ *$/d; s/[^:]*: *//' | paste -d, - - -

输出:

4/11/2013 12:00:48 AM,MDS error,Errors
4/11/2013 12:03:27 AM,MDS error,Errors
4/11/2013 12:05:39 AM,MDS error,Errors

如果你有一个可变数量的行,你可以用GNU awk这样做(也许是mawk):

<infile awk 'NF>0 {gsub("\n\n+", "\n"); gsub("\n[^:]+: *", ","); sub(",",""); print}' RS='-{40}' ORS=''

第一个替换删除空行,第二个替换用逗号替换标题,第三个替换删除无关的逗号。

答案 5 :(得分:0)

这是我的:

sed -ne '/----/{N;N;N;s/\n/,/g;s/[^:]*: \([^,]*,\)[^:]*: \([^,]*,\)[^:]*: \(.*\)/\1\2\3/;p;}' file

这确实假设在虚线后面有三条感兴趣的线。如果它是可变的,则必须进行一些循环。

答案 6 :(得分:0)

awk -F ": " '!(i%3)&&i{print s;s=i=""}/#/{s=s!=""?s","$2:$2;i++}'