Linux中grep多个字符串

时间:2020-02-23 14:44:58  来源:igfitidea点击:

在本教程中,我们将看到如何在Linux中获得多个字符串。

假设应用程序部署在Linux机器上,我们需要对日志文件进行分析,我们需要查找具有特定单词的行。
例如:我们希望其中找到"警告"或者"调试"单词的所有行。

我将尝试在exampleR的帮助下提供这些命令。
我创建了一个我们将用于我们的命令的示例日志文件。
示例日志文件ApplicationLog.log

:: Spring Boot :: (v1.5.3.RELEASE)2016-04-28 14:20:10.519 INFO 1051 — [ main] o.a.j.SpringBootHelloWorldApplication : Starting SpringBootHelloWorldApplication on apples-MacBook-Air.local with PID 1051 (/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/classes started by apple in /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample)
2016-04-28 14:20:12.697 ERROR 1051 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-04-28 14:20:12.720 DEBUG 1051 — [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-04-28 14:20:12.725 INFO 1051 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2016-04-28 14:20:13.227 INFO 1051 — [ost-startStop-1] org.apache.jasper.servlet.comScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2016-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2629 ms
2016-04-28 14:20:13.436 WARNING 1051 — [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: ‘dispatcherServlet’ to [/]
2016-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter’ to: [/*]
2016-04-28 14:20:13.807 DEBUG 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c624d60: startup date [Fri Apr 28 14:20:10 IST 2016]; root of context hierarchy
2016-04-28 14:20:13.921 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/helloworld]}” onto public org.springframework.web.servlet.ModelAndView org.igi.theitroad.springboot.HelloWorldController.hello()
2016-04-28 14:20:13.927 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error]}” onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-04-28 14:20:13.928 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error],produces=[text/html]}” onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse)
2016-04-28 14:20:13.966 INFO-DEBUG 1051 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

1.命令在ApplicationLog.log文件中找到"错误"或者"debug"

$grep'debug \ |ERROR'applicationlog.log

2016-04-28 14:20:12.697错误1051  -  [Main] SBCettomCatembeddedServletContainer:Tomcat与端口初始化:8080(http)2016-04-28 14:20:12.720调试1051  -  [Main] O.apache .catalina.core.standardservice:启动服务Tomcat 2016-04-28 14:20:13.807 Debug 1051  -  [Main] SwSmmareQuestMappingHandlerAdapter:查找@ControllEradvice:Org.springFramework.Boot.context.embedded.annotationConfigemBeddedWebApplicationContext@4C624D60:启动日期[星期五4月28日14:20:10 IST 2016];上下文层次结构2016-04-28 14:20:13.966信息调试1051  -  [main] oswshandler.simpleurlhandlermapping:type [class org.springframework.web.servlet的处理程序上的映射URL路径[/webjars/**] .resource.resourcehttpereesthandler]

如果要避免转义字符,我们也可以使用以下命令。

$egrep'debug |ERROR'applicationlog.log

2.命令在ApplicationLog.log文件中找到"Info"和"Debug"。

$grep'INFO.* debug \ |DEBUG。* info'applicationlog.log

2016-04-28 14:20:13.966信息调试1051  -  [main] oswshandler.simpleurlhandlermapping:type [class org.springframework.web.servlet.resource.resourcehttprequesthandler ]

3.使用ApplicationLog.log文件中的行号查找"信息"和"Debug"的命令。

我们可以使用-n选项显示行号。

$grep-n'信息。* debug \ |DEBUG。* info'applicationlog.log

21:2016-04-28 14:20:13.966信息调试1051  -  [main] oswshandler.simpleurlhandlermapping:type [class org.springframework.web.servlet.resource ove to type [class org.springframework.web.servlet.resource上的映射URL路径[/webjars/**] .resourcehttprequesthandler]

4. 4.command在applicationlog.log文件中找到"错误"或者"调试"Witn Quation在ApplicationLog.log文件中不敏感

我们可以使用-i选项来实现案例不敏感。

### $grep -i'debug \ |ERROR'applicationlog.log

5.command在applicationlog.log文件中当前文件夹中的所有日志文件中找到"错误"或者"调试"

### $grep'debug \|ERROR'* .log