我收到了一个日志文件(见下文),我需要使用bash脚本将其设置为这种格式:
title pdfspool date rip date bmpspool date CLAB date Sometitle12 10/09/23 00:56:40 10/9/23 0:56:46 10/9/23 0:56:50 10/9/23 1:01:13
日志文件
!!Begin Source aserver:pdf_spool:the, Job 844b015e0043469e, Inst 844b015e0043469e Title Sometitle12.pdf Action Started Received, Ok Date 10/09/23 00:56:40 For Administrator (8) DataType = PDF (17) Source = srv01:aserver:file_input:0 !!End !!Begin Source aserver:rip:rip1, Job 844b015e0043469e, Inst 844b015e004346a0 Title Sometitle12.pdf Cyan 1 Action Started Transmit, Ok Date 10/09/23 00:56:46 For Administrator (8) DataType = Bitmap (1) Destination = srv01:bserver:bmp_spool:the (4) Parent = 844b015e0043469e/844b015e0043469e !!End !!Begin Source bserver:bmp_spool:the, Job 844b015e0043469e, Inst 844b015e004346a0 Title Sometitle12.pdf Cyan 1 Action Started Received, Ok Date 10/09/23 00:56:50 For Administrator (8) DataType = Bitmap (17) Source = srv01:aserver:rip:rip1 !!End !!Begin Source bserver:bmp_spool:the, Job 844b015e0043469e, Inst 844b015e004346a0 Title Sometitle12.pdf Cyan 1 Action Atomic Accepted, Ok Date 10/09/23 01:01:13 For Administrator (8) DataType = Bitmap (2) Source Queue = ^03Newspaper ltd(MP)^Date - 24MP^Site - N^ (5) Requested By = clab (15) Approval Status = Waiting Approved Changed from Waiting to Approved by clab. !!End
欢迎提示。
谢谢!
答案 0 :(得分:2)
使用awk。写一个状态机。当您看到/^!!Begin$/
时,切换状态,记录您的数据,转储输出并在看到/^!!End$/
时切换回来。
答案 1 :(得分:2)
awk 'BEGIN{}
/Action Started Received/ && !c{ pdfspooldate=$(NF-1)$NF ;c++}
/Action Started Received/ && c{ bmppooldate=$(NF-1)$NF ;c=0}
/Action Started Transmit/{ ripdate=$(NF-1)$NF }
/title/ { title=$2}
/Action Atomic Accepted/{ clabdate=$(NF-1)$NF }
END{ print title,pdfspooldate,ripdate,clabdate }' file
答案 2 :(得分:0)
如果使用Perl / Python / Ruby,则应该能够在一行(匹配部分)中使用正则表达式匹配。使用多行模式,其中.
将与换行符匹配。我认为awk或sed应该能够以相同的方式使用正则表达式:
例如,在Ruby中:
s = <<TEXT
!!Begin
Something haha
Title Good Bad Ugly
Date 1/1/2008
!!End
!!Begin
Other info
Title Iron Man
Date 2/2/2010
TEXT
result = s.scan(/^!!Begin.*?^Title\s+([^\n]*).*?^Date\s+([^\n]*)/m)
p result
result.each do |arr|
puts arr.join(' ')
end
输出:
$ ruby try.rb
[["Good Bad Ugly", "1/1/2008"], ["Iron Man", "2/2/2010"]]
Good Bad Ugly 1/1/2008
Iron Man 2/2/2010
答案 3 :(得分:0)
我将Perl与$/ = "!!End"
一起使用,然后解析每个段落。