Using grep to search a file using multiple words as boolean AND OR condition

Command Overview

The following extended grep  (grep -E) example shows how to search a log files for

  1. Any line containing the words
    1. init
      AND
    2. SNMP OR tomcat 
  2. Any line containing either
    1. start_tomcat
      OR
    2. stop_tomcat
      OR
    3. start_snmp
      OR
    4. stop_snmp
  3. excluding any match with the word INT

Example

# grep -E "init.*(snmp|tomcat)|(start|stop)_(tomcat|snmp)" debug.log |grep -v INT 2024-09-10T13:54:36.686053-07:00 nec-6090-01#b stop_tomcat[4019]: running, pid file: /var/run/jsvc.pid , timeout interval: 15 secs 2024-09-10T13:54:36.687429-07:00 nec-6090-01#b stop_tomcat[4019]: pid: 1651 2024-09-10T13:54:36.704588-07:00 nec-6090-01#b stop_tomcat[4019]: init-+-AuthenticationS---7*[{Authentication}] 2024-09-10T13:54:36.705498-07:00 nec-6090-01#b stop_tomcat[4019]: |-HAMon---15*[{HAMon}] 2024-09-10T13:54:36.706346-07:00 nec-6090-01#b stop_tomcat[4019]: |-PlatMon---13*[{PlatMon}] 2024-09-10T13:54:36.707142-07:00 nec-6090-01#b stop_tomcat[4019]: |-ProcMon-+-realstore---98*[{realstore}] 2024-09-10T13:54:36.707881-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.708690-07:00 nec-6090-01#b stop_tomcat[4019]: `-9*[{ProcMon}] 2024-09-10T13:54:36.709454-07:00 nec-6090-01#b stop_tomcat[4019]: |-agetty 2024-09-10T13:54:36.710315-07:00 nec-6090-01#b stop_tomcat[4019]: |-cimserver---2*[{cimserver}] 2024-09-10T13:54:36.711077-07:00 nec-6090-01#b stop_tomcat[4019]: |-corewatch 2024-09-10T13:54:36.711792-07:00 nec-6090-01#b stop_tomcat[4019]: |-crond 2024-09-10T13:54:36.712541-07:00 nec-6090-01#b stop_tomcat[4019]: |-dbus-daemon 2024-09-10T13:54:36.713388-07:00 nec-6090-01#b stop_tomcat[4019]: |-java---21*[{java}] 2024-09-10T13:54:36.714237-07:00 nec-6090-01#b stop_tomcat[4019]: |-jsvc-+-jsvc 2024-09-10T13:54:36.714977-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.715776-07:00 nec-6090-01#b stop_tomcat[4019]: `-jsvc---62*[{jsvc}] 2024-09-10T13:54:36.716540-07:00 nec-6090-01#b stop_tomcat[4019]: |-jsvc-+-jsvc 2024-09-10T13:54:36.717367-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.718192-07:00 nec-6090-01#b stop_tomcat[4019]: `-jsvc---109*[{jsvc}] 2024-09-10T13:54:36.720254-07:00 nec-6090-01#b stop_tomcat[4019]: |-lldpd---lldpd 2024-09-10T13:54:36.721010-07:00 nec-6090-01#b stop_tomcat[4019]: |-6*[mingetty] 2024-09-10T13:54:36.721717-07:00 nec-6090-01#b stop_tomcat[4019]: |-ntpd---ntpd 2024-09-10T13:54:36.722473-07:00 nec-6090-01#b stop_tomcat[4019]: |-rpcbind 2024-09-10T13:54:36.723252-07:00 nec-6090-01#b stop_tomcat[4019]: |-rsyslogd-+-log_scrub.py 2024-09-10T13:54:36.724039-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.724822-07:00 nec-6090-01#b stop_tomcat[4019]: `-9*[{rsyslogd}] 2024-09-10T13:54:36.725621-07:00 nec-6090-01#b stop_tomcat[4019]: |-runuser---postmaster---18*[postmaster] 2024-09-10T13:54:36.726416-07:00 nec-6090-01#b stop_tomcat[4019]: |-runuser---postmaster---16*[postmaster] 2024-09-10T13:54:36.727143-07:00 nec-6090-01#b stop_tomcat[4019]: |-sh---stop_tomcat---pstree 2024-09-10T13:54:36.727860-07:00 nec-6090-01#b stop_tomcat[4019]: |-sshd-+-sshd---rsync.bin 2024-09-10T13:54:36.728562-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.729270-07:00 nec-6090-01#b stop_tomcat[4019]: `-sshd---bash---service---tomcat---initctl 2024-09-10T13:54:36.730039-07:00 nec-6090-01#b stop_tomcat[4019]: |-thriftshell---11*[{thriftshell}] 2024-09-10T13:54:36.730807-07:00 nec-6090-01#b stop_tomcat[4019]: |-udevd---2*[udevd] 2024-09-10T13:54:36.731537-07:00 nec-6090-01#b stop_tomcat[4019]: |-watchdog 2024-09-10T13:54:36.732229-07:00 nec-6090-01#b stop_tomcat[4019]: `-winbindd---winbindd 2024-09-10T13:54:37.735365-07:00 nec-6090-01#b stop_tomcat[4019]: Tomcat is stopped 2024-09-10T13:54:37.738757-07:00 nec-6090-01#b init[4098]: tomcat post-start 2024-09-10T13:54:39.366904-07:00 nec-6090-01#b start_tomcat[4097]: LOG-TOMCAT-0003: TASKSET options 2024-09-10T13:54:39.369087-07:00 nec-6090-01#b start_tomcat[4097]: Set JAVA options -Xms128M -Xmx2800M -XX:MaxMetaspaceSize=140M -XX:ReservedCodeCacheSize=140M -XX:+UseCodeCacheFlushing -Xss512k -XX:+UseStringDeduplication -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=*:5005 -XX:+PrintClassHistogram -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/corefiles -XX:ErrorFile=/var/corefiles/hs_err_pid<>.log -Dfile.encoding=UTF8 -Dcom.tintri.platform.productid=BR2 -Dhttps.protocols=TLSv1.2 -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false 2024-09-10T13:54:39.396222-07:00 nec-6090-01#b start_tomcat[4097]: Starting server using jsvc. 2024-09-10T13:55:11.215363-07:00 nec-6090-01#b stop_snmp[15117]: Stopping snmp agent 2024-09-10T13:55:12.217316-07:00 nec-6090-01#b stop_snmp[15117]: SNMP agent is stopped 2024-09-10T13:55:12.246235-07:00 nec-6090-01#b start_snmp[15416]: Set JAVA options -Xms64M -Xmx128M -XX:MaxMetaspaceSize=48M -XX:ReservedCodeCacheSize=48M -XX:+UseCodeCacheFlushing -Xss512k -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=*:5006 -XX:+UseStringDeduplication -XX:+PrintClassHistogram -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/corefiles -Dcom.tintri.platform.productid=BR2 -Dfile.encoding=UTF8 2024-09-10T13:55:12.247242-07:00 nec-6090-01#b start_snmp[15416]: Starting SNMP agent using jsvc. 2024-09-10T14:27:39.615636-07:00 nec-6090-01#b vmstore[2582]: HA: [2961] [tid 4996] HA-STATE: errorDetected: module [1], current state [104] indicates Error 2024-09-10T14:27:39.615698-07:00 nec-6090-01#b vmstore[2582]: HA: [2962] [tid 4996] HA-STATE: setStatusLocked: setting nodeRole to [SECONDARY], nodeStatus to [DISCONNECTED] 2024-09-10T14:27:39.690762-07:00 nec-6090-01#b vmstore[2582]: HA: [2967] [tid 5043] HA-STATE: errorDetected: module [5], current state [104] indicates Error 2024-09-10T14:27:40.191327-07:00 nec-6090-01#b vmstore[2582]: HA: [2969] [tid 5020] HA-STATE: setStatusLocked: setting nodeRole to [PRIMARY], nodeStatus to [SELECTED] 2024-09-10T14:27:40.312790-07:00 nec-6090-01#b vmstore[2582]: HA: [2970] [tid 2911] HA-STATE: disconnecting Secondary; becoming Primary 2024-09-10T14:27:40.313379-07:00 nec-6090-01#b vmstore[2582]: HA: [2980] [tid 2911] HA-STATE: notified HA modules to become Primary 2024-09-10T14:27:40.914011-07:00 nec-6090-01#b vmstore[2582]: HA: [3672] [tid 2911] HA-STATE: becomePrimary: invoked 2024-09-10T14:27:40.914059-07:00 nec-6090-01#b vmstore[2582]: HA: [3673] [tid 2911] HA-STATE: becomePrimary: eligible to be Primary due to NVRAM gen num [4] >= current disk gen num [4] 2024-09-10T14:27:40.914099-07:00 nec-6090-01#b vmstore[2582]: HA: [3674] [tid 2911] HA-STATE: updateGenNum: STARTUP: writing intended gen num [5] 2024-09-10T14:27:40.956406-07:00 nec-6090-01#b vmstore[2582]: HA: [3676] [tid 2911] HA-STATE: updateGenNum: writing NVRAM gen num as [5] 2024-09-10T14:27:40.999307-07:00 nec-6090-01#b vmstore[2582]: HA: [3681] [tid 2911] HA-STATE: setStatusLocked: setting nodeRole to [PRIMARY], nodeStatus to [RECOVERING] 2024-09-10T14:27:46.172490-07:00 nec-6090-01#b vmstore[2582]: HA: [7846] [tid 2911] HA-STATE: primaryBeginAccepting: invoked 2024-09-10T14:27:46.172972-07:00 nec-6090-01#b vmstore[2582]: HA: [7856] [tid 2911] HA-STATE: setStatusLocked: setting nodeRole to [PRIMARY], nodeStatus to [ACTIVE] 2024-09-10T14:27:46.173513-07:00 nec-6090-01#b vmstore[2582]: HA: [7868] [tid 746] HA-STATE: haManagerListenerThread: invoked 2024-09-10T14:28:46.213586-07:00 nec-6090-01#b vmstore[2582]: HA: [10685] [tid 746] HA-STATE: syncComplete: module [5] indicates SyncComplete 2024-09-10T14:28:47.113674-07:00 nec-6090-01#b vmstore[2582]: HA: [10815] [tid 4997] HA-STATE: syncComplete: module [1] indicates SyncComplete
This entry was posted in Command Line FU, Linux, Uncategorized. Bookmark the permalink.