我有一个这样的电子邮件日志文件:
2013-09-11 12:02:08 INFO: ------------------------------
2013-09-11 12:02:08 INFO: Javamail session sending email
2013-09-11 12:02:08 INFO: Session properties:
2013-09-11 12:02:08 INFO: com.hof.email.starttime=20130911120208
2013-09-11 12:02:08 INFO: mail.smtp.auth=true
2013-09-11 12:02:08 INFO: mail.smtp.connectiontimeout=60000
2013-09-11 12:02:08 INFO: mail.smtp.host=mailserver
2013-09-11 12:02:08 INFO: mail.smtp.port=25
2013-09-11 12:02:08 INFO: mail.smtp.timeout=60000
2013-09-11 12:02:08 INFO: mail.transport.protocol=smtp
2013-09-11 12:02:08 INFO: From: Support
2013-09-11 12:02:08 INFO: To: Customer
2013-09-11 12:02:08 INFO: Subject: Your Report Data
2013-09-11 12:02:08 INFO: Message ID: <id>
2013-09-11 12:02:09 INFO: Email sent successfully
2013-09-11 12:02:09 INFO: Javamail session ended
2013-09-11 12:02:09 INFO: ------------------------------
如果电子邮件主题与特定字符串匹配,我需要做的是打印整个记录。
也就是说,我认为我想做的是,当Subject = 'Your Report Data'
时,然后打印'------------------------------'
的第n次出现和'------------------------------'
的第1次出现之间的所有行。来自主题匹配。
答案 0 :(得分:2)
如果行之间的部分始终相同,则可以将grep
与-A
和-B
一起使用。
答案 1 :(得分:1)
这仅适用于gawk
awk '/Subject: Your Report Data/{printf "%s%s\n",$0,RT}' RS="------------------------------" file
编辑:更复杂的版本,打印正确的部分
cat file
2013-09-11 12:02:08 INFO: ------------------------------
2013-09-11 12:02:08 INFO: Javamail session sending email
2013-09-11 12:02:08 INFO: Session properties:
2013-09-11 12:02:08 INFO: com.hof.email.starttime=20130911120208
2013-09-11 12:02:08 INFO: mail.smtp.auth=true
2013-09-11 12:02:08 INFO: mail.smtp.connectiontimeout=60000
2013-09-11 12:02:08 INFO: mail.smtp.host=mailserver
2013-09-11 12:02:08 INFO: mail.smtp.port=25
2013-09-11 12:02:08 INFO: mail.smtp.timeout=60000
2013-09-11 12:02:08 INFO: mail.transport.protocol=smtp
2013-09-11 12:02:08 INFO: From: Support
2013-09-11 12:02:08 INFO: To: Customer
2013-09-11 12:02:08 INFO: Subject: Your Report Data
2013-09-11 12:02:08 INFO: Message ID: <id>
2013-09-11 12:02:09 INFO: Email sent successfully
2013-09-11 12:02:09 INFO: Javamail session ended
2013-09-11 12:02:09 INFO: ------------------------------
2013-09-11 12:02:08 INFO: Javamail session sending email
2013-09-11 12:02:08 INFO: Session properties:
2013-09-11 12:02:08 INFO: com.hof.email.starttime=20130911120208
2013-09-11 12:02:08 INFO: mail.smtp.auth=true
2013-09-11 12:02:08 INFO: mail.smtp.connectiontimeout=60000
2013-09-11 12:02:08 INFO: mail.smtp.host=mailserver
2013-09-11 12:02:08 INFO: mail.smtp.port=25
2013-09-11 12:02:08 INFO: mail.smtp.timeout=60000
2013-09-11 12:02:08 INFO: mail.transport.protocol=smtp
2013-09-11 12:02:08 INFO: From: Support
2013-09-11 12:02:08 INFO: To: Customer
2013-09-11 12:02:08 INFO: Subject: Error
2013-09-11 12:02:08 INFO: Message ID: <id>
2013-09-11 12:02:09 INFO: Email sent successfully
2013-09-11 12:02:09 INFO: Javamail session ended
2013-09-11 12:02:09 INFO: ------------------------------
awk '/---/ {if (p) {for (j=0;j<i;j++) print a[j]};i=0;p=0;delete a;a[i++]=$0} !/---/ {a[i++]=$0} /Your/ {p=1}'
2013-09-11 12:02:08 INFO: ------------------------------
2013-09-11 12:02:08 INFO: Javamail session sending email
2013-09-11 12:02:08 INFO: Session properties:
2013-09-11 12:02:08 INFO: com.hof.email.starttime=20130911120208
2013-09-11 12:02:08 INFO: mail.smtp.auth=true
2013-09-11 12:02:08 INFO: mail.smtp.connectiontimeout=60000
2013-09-11 12:02:08 INFO: mail.smtp.host=mailserver
2013-09-11 12:02:08 INFO: mail.smtp.port=25
2013-09-11 12:02:08 INFO: mail.smtp.timeout=60000
2013-09-11 12:02:08 INFO: mail.transport.protocol=smtp
2013-09-11 12:02:08 INFO: From: Support
2013-09-11 12:02:08 INFO: To: Customer
2013-09-11 12:02:08 INFO: Subject: Your Report Data
2013-09-11 12:02:08 INFO: Message ID: <id>
2013-09-11 12:02:09 INFO: Email sent successfully
2013-09-11 12:02:09 INFO: Javamail session ended
答案 2 :(得分:1)
为了好玩,这里有一个GNU grep方法,通过多行搜索来解决这个问题。有关此方法的详细信息great answer
grep -ozP '(?s)(?<=--\n).*?Subject: Your Report Data.*?(?=\n\N*?--)'
答案 3 :(得分:0)
对于不同数量的行,您也可以使用此Ruby代码:
ruby -e 'exp = Regexp.new("^[^\n]+INFO: -{30}$.*?INFO: Subject: #{Regexp.escape(ARGV.shift)}$.*?-{30}$", Regexp::MULTILINE); File.read(ARGV.shift).scan(exp).each{|e| puts e}' "Your Report Data" file
它不会将主题中的字符解释为正则表达式字符。