简体   繁体   English

bash脚本,打印匹配特定样式的多行

[英]bash script, printing multiple lines matching to a specific patern

I have a log file and I'm making a script to do some actions. 我有一个日志文件,并且正在编写脚本来执行一些操作。 An action is to print a specific area of the log. 一个动作是打印日志的特定区域。

 03:19:13.4 End summary update for ads.doc.ordered.OrderDetailSummary
 03:19:13.4 Begin summary update for ads.doc.inventory.InventoryItemSummary
 03:19:33.9 CronServer:: DailyJob ads.tools.UpdateSummaries@17c5d6cf failed with exception
  ads.util.AppError: Cannot create UnitName from keys: Each
  ads.util.AppError: Cannot create UnitName from keys: Each
  .....
  .....
  .....
  .....
 03:46:42.5 Periodic support request failed: ads.support.SupportException: Error opening socket: java.net.ConnectException: Connection refused
 06:31:36.1 Upload failed: java.io.FileNotFoundException: c:/tmp/cygwin1.dll (No such file or directory)
 08:01:08.0 connect from /172.22.3.28

OR 要么

06:14:27.9 starting web server
06:14:33.3 Initializing Spring framework Logs
Oct 18, 2013 6:14:33 AM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Oct 18, 2013 6:14:34 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
Oct 18, 2013 6:14:35 AM org.apache.catalina.startup.ContextConfig defaultWebConfig
INFO: No default web.xml
Oct 18, 2013 6:14:38 AM org.apache.catalina.session.StandardManager doLoad
SEVERE: IOException while loading persisted sessions: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: ads.doc.backoffice.StoreInfos
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: ads.doc.backoffice.StoreInfos
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
......
......
......
......

    at ads.tools.AppServerMain.main(AppServerMain.java:83)
Caused by: java.io.NotSerializableException: ads.doc.backoffice.StoreInfos
INFO: Jk running ID=0 time=0/105  config=null
06:14:48.6 Starting exporter server
06:14:48.6 starting cron server

I want to print the block that contains the word exception. 我要打印包含单词异常的块。 From a time stamp to the next time stamp 从时间戳记到下一个时间戳记

e.g1 例如

03:19:33.9 CronServer:: DailyJob ads.tools.UpdateSummaries@17c5d6cf failed with exception ads.util.AppError: Cannot create UnitName from keys: Each
ads.util.AppError: Cannot create UnitName from keys: Each
.....
.....
.....
.....

and the next timestamp if it's easier. 和下一个时间戳(如果更简单)。

OR 要么

e.g2 例如2

06:14:33.3 Initializing Spring framework Logs
......
......
......

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: ads.doc.backoffice.StoreInfos
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
......
......
......
......

    at ads.tools.AppServerMain.main(AppServerMain.java:83)
Caused by: java.io.NotSerializableException: ads.doc.backoffice.StoreInfos
INFO: Jk running ID=0 time=0/105  config=null
06:14:48.6 Starting exporter server

Is there a way to do this with sed? 有办法用sed做到这一点吗? Or any other idea because I really got confused. 或其他任何想法,因为我真的很困惑。

As I know sed process a single line at a time. 据我所知sed一次只能处理一行。

Thought to search if a line starts with this pattern 想搜索一行是否以此模式开头

^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}.[0-9]

and then there is a match with the exception word and after the next line matches again the expression to print all this block. 然后有一个与异常词匹配的内容,并且在下一行之后再次匹配该表达式以打印所有此块。

The critical lines seem not to begin with a date, so you could search for your error condition or lines not beginning with a date like this: 关键行似乎不是以日期开头的,因此您可以搜索错误情况或不是以日期开头的行,如下所示:

egrep -i "exception|^\s" yourlog

where the "\\s" is a space. 其中“ \\ s”是一个空格。

Or alternatively, lines not beginning with a digit: 或者,也可以不以数字开头的行:

egrep -i "exception|^[^0-9]"

You can say: 你可以说:

sed '/exception/,/^[0-9][0-9]/!d' filename

For your input, it'd return: 对于您的输入,它将返回:

03:19:33.9 CronServer:: DailyJob ads.tools.UpdateSummaries@17c5d6cf failed with exception
ads.util.AppError: Cannot create UnitName from keys: Each
ads.util.AppError: Cannot create UnitName from keys: Each
.....
.....
.....
.....
03:46:42.5 Periodic support request failed: ads.support.SupportException: Error opening socket: java.net.ConnectException: Connection refused

EDIT : For your updated question, the following might work for you: 编辑 :对于您的更新的问题,以下可能适用于您:

sed -n -e :t -e '/^[0-9]/,/^[0-9]/{/^[0-9]/!{$!{N;bt}};/[Ex]xception/p;}' inputfile
 perl -n -e 'if ( $_ =~ /^ \d{2}:\d{2}:\d{2}\.\d/ ) { $print = 0; } if ($print || $_ =~ /exception/) { print "$_"; $print = 1 };

Something like that should be the perl alternative. 类似的东西应该是perl的替代方案。

Use like that cat file.txt | perl ... cat file.txt | perl ...使用cat file.txt | perl ... cat file.txt | perl ...

Sed has ranges by patterns: http://www.grymoire.com/Unix/Sed.html#uh-29 Sed的模式范围不同: http : //www.grymoire.com/Unix/Sed.html#uh-29

So you could do something like: 因此,您可以执行以下操作:

 sed -n '/AppError/,/^ *[0-9]\{1,2\}:[0-9]\{1,2\}:[0-9]\{1,2\}\.[0-9]/ p' file.log

-n ... to suppress printing unless you print explicitly You can refine the start pattern to your liking. -n ...禁止打印,除非您明确打印。您可以根据自己的喜好来优化开始模式。

In your end pattern, you need to escape the curly braces. 在结束模式中,您需要避免使用花括号。

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

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