1

我有如下文件:

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

4

7 回答 7

5
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
于 2013-04-12T07:11:19.547 回答
1

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

sed '/^#Timestamp:/{N;N;y/\n/,/;s/#[^ ]* //gp};d' file
于 2013-04-12T00:58:36.507 回答
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
于 2013-04-11T18:44:48.483 回答
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
于 2013-04-11T18:44:14.077 回答
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=''

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

于 2013-04-11T21:00:11.003 回答
0

这是我的:

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

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

于 2013-04-11T21:21:26.840 回答
0
awk -F ": " '!(i%3)&&i{print s;s=i=""}/#/{s=s!=""?s","$2:$2;i++}'
于 2013-04-16T09:53:30.623 回答