grep multiple Strings in linux

Next

In this post, we will see how to grep multiple Strings in linux.

Let’s say your application is deployed on linux machine and you need do analysis of log file and you need to find lines with the specific words in it.
For example: you want to find all the lines with “WARNING” or “DEBUG” words in it.

I will try to provide these commands with the help of examples.I have created a sample log file which we are going to use for our commands.
Sample log file applicationLog.log

:: Spring Boot :: (v1.5.3.RELEASE)2017-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)
2017-04-28 14:20:12.697 ERROR 1051 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-28 14:20:12.720 DEBUG 1051 — [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-28 14:20:12.725 INFO 1051 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-04-28 14:20:13.227 INFO 1051 — [ost-startStop-1] org.apache.jasper.servlet.TldScanner : 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.
2017-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2629 ms
2017-04-28 14:20:13.436 WARNING 1051 — [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: ‘dispatcherServlet’ to [/]
2017-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter’ to: [/*]
2017-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 2017]; root of context hierarchy
2017-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.arpit.java2blog.springboot.HelloWorldController.hello()
2017-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)
2017-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)
2017-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. Command to find “ERROR” or “DEBUG” in applicationLog.log file

$ grep ‘DEBUG\|ERROR’ applicationLog.log

2017-04-28 14:20:12.697 ERROR 1051 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-28 14:20:12.720 DEBUG 1051 — [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-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 2017]; root of context hierarchy
2017-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]

You can use follow command as well if want to avoid escape character.

$ egrep ‘DEBUG|ERROR’ applicationLog.log


2. Command to find “INFO” and “DEBUG” in applicationLog.log file.

$ grep ‘INFO.*DEBUG\|DEBUG.*INFO’ applicationLog.log

2017-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]


3. Command to find “INFO” and “DEBUG” with line number in applicationLog.log file.

You can use -n option to display line numbers.

$ grep -n ‘INFO.*DEBUG\|DEBUG.*INFO’ applicationLog.log

21:2017-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]


4.Command to find “ERROR” or “DEBUG” witn case insensitive in applicationLog.log file in applicationLog.log file

You can use -i option to achive case insensesitive.

$ grep -i ‘DEBUG\|ERROR’ applicationLog.log


5.Command to find “ERROR” or “DEBUG” in all logs files in the current folder in applicationLog.log file

$ grep ‘DEBUG\|ERROR’ *.log

That’s all about grep multiple Strings in linux.

Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog