简体   繁体   English

将多行日志文件转换为CSV

[英]Converting multi-line log file to CSV

I have file which looks like following: 我有如下文件:

----------------------------------------
#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

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

I need to convert it to CSV file which looks like this: 我需要将其转换为如下所示的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

Need something being done as a command line (awk/sed)? 需要作为命令行执行某些操作(awk / sed)吗? since I have a series of files like this one which need to be converted to CSV. 因为我有一系列这样的文件,需要将其转换为CSV。

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

Tested: 经过测试:

> 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

A shorter solution if its ok for the OP: 一个较短的解决方案,如果它可以用于OP:

awk -F: '/^#/{line=line","$2}/^-/{print substr(line,3);line="";}' your_file
#!/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

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

sed '/^#Timestamp:/{N;N;y/\n/,/;s/#[^ ]* //gp};d' file
$ 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

Assuming each record only contains three rows, you can get away with cleaning the input and "pasting" it together: 假设每个记录仅包含三行,则可以清理输入并将它们“粘贴”在一起:

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

Output: 输出:

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

If you have a variable number of rows, you could do it like this with GNU awk (perhaps mawk as well): 如果行数可变,则可以使用GNU awk(也可能是mawk)这样做:

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

The first substitution removes empty lines, the second replaces headers with comma, and the third removes an extraneous comma. 第一个替换将删除空行,第二个替换将标题替换为逗号,第三个替换将删除多余的逗号。

Here's mine: 这是我的:

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

That does assume there are three lines of interest following the dashed line. 那确实假设在虚线之后有三条感兴趣的线。 If it's variable, some looping would have to happen. 如果它是变量,则必须进行一些循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM