使用 awk 将基于记录(多行)的文本转换为 csv

使用 awk 将基于记录(多行)的文本转换为 csv

我的输入文件如下所示:

Session: 324098324
Object: dd02:/24
Time: Sat 28 Oct 2017 12:40:41 AM CEST
Status: Error
ID: 5F2B0121-4F2B-481D-B79F-2DC827B85093/16123991

Session: 324321
Object: ad02:/24
Time: Sun 13 May 2018 12:00:43 PM CEST
Status: Ok
ID: 5F2B0121-4F2B-481D-B79F-2DC827B85093/21857092

Session: 132452
Object: xd01:/24
Time: Sun 08 Jul 2018 12:16:56 PM CEST
Status: Error
ID: 5F2B0121-4F2B-481D-B79F-2DC827B85093/21695878

输出应该是:[错误]

Session: 324098324, Object: ad02:/24, Time: Sat 28 Oct 2017 12:40:41 AM CEST, Status: Error, ID: 5F2B0121-4F2B-481D-B79F-2DC827B85093/16123991
Session: 324098324, Object: ad02:/24, Time: Sat 28 Oct 2017 12:40:41 AM CEST, Status: Error, ID: 5F2B0121-4F2B-481D-B79F-2DC827B85093/16123991
Session: 324098324, Object: ad02:/24, Time: Sat 28 Oct 2017 12:40:41 AM CEST, Status: Error, ID: 5F2B0121-4F2B-481D-B79F-2DC827B85093/16123991

编辑:我在输出格式上犯了一个大错误,上面的格式不能用作 CSV,我需要一种如下格式:

Session, Object, Time, Status, ID
324098324, ad02:/24, Sat 28 Oct 2017 12:40:41 AM CEST, Error, 5F2B0121-4F2B-481D-B79F-2DC827B85093/16123991
324098324, ad02:/24, Sat 28 Oct 2017 12:40:41 AM CEST, Error, 5F2B0121-4F2B-481D-B79F-2DC827B85093/16123991
324098324, ad02:/24, Sat 28 Oct 2017 12:40:41 AM CEST, Error, 5F2B0121-4F2B-481D-B79F-2DC827B85093/16123991

答案1

awk -v RS='' '{gsub("\n", ", "); print}' input

答案2

paste可用的?尝试

paste -s -d"\t\t\t\t\t\n" file

答案3

grep -A4 "Session " myfile | paste -s -d ' \n'

grep找到word session,成功后打印4行。

paste将它们转换为单行。

您可以通过以下方式轻松设置输出分隔符paste -s -d '||||\n' OR paste -s -d ',,,,\n'

相关内容