Search multiple pattern in a file

I have a sample file with following output:

HTTP/1.1 200 OK
User: admin
Set-Cookie: AMBARISESSIONID=y3v3648yqcno32nq478kw7ar;Path=/;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: text/plain
Vary: Accept-Encoding, User-Agent
Content-Length: 6057
Server: Jetty(8.1.17.v20150415)

{
  "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN",
  "ServiceInfo" : {
    "cluster_name" : "XXXXX",
    "maintenance_state" : "OFF",
    "service_name" : "YARN",
    "state" : "STARTED"
  },
  "alerts_summary" : {
    "CRITICAL" : 7,
    "MAINTENANCE" : 0,
    "OK" : 2,
    "UNKNOWN" : 3,
    "WARNING" : 0
  },
  "alerts" : [
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/44",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 4,
        "definition_name" : "yarn_nodemanager_webui_percent",
        "host_name" : null,
        "id" : 44,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/57",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 8,
        "definition_name" : "yarn_nodemanager_webui",
        "host_name" : "eu-lamp-dev-m-0021-hadoop-slave01",
        "id" : 57,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/58",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 7,
        "definition_name" : "yarn_nodemanager_health",
        "host_name" : "eu-lamp-dev-m-0021-hadoop-slave01",
        "id" : 58,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/49",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 8,
        "definition_name" : "yarn_nodemanager_webui",
        "host_name" : "eu-lamp-dev-m-0022-hadoop-slave02",
        "id" : 49,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/50",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 7,
        "definition_name" : "yarn_nodemanager_health",
        "host_name" : "eu-lamp-dev-m-0022-hadoop-slave02",
        "id" : 50,
       "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/40",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 8,
        "definition_name" : "yarn_nodemanager_webui",
        "host_name" : "eu-lamp-dev-m-0023-hadoop-slave03",
        "id" : 40,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/41",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 7,
        "definition_name" : "yarn_nodemanager_health",
        "host_name" : "eu-lamp-dev-m-0023-hadoop-slave03",
        "id" : 41,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/2",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 2,
        "definition_name" : "yarn_resourcemanager_webui",
        "host_name" : "eu-lamp-dev-xl-0018-hadoop-master",
        "id" : 2,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/5",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 5,
        "definition_name" : "nodemanager_health_summary",
        "host_name" : "eu-lamp-dev-xl-0018-hadoop-master",
        "id" : 5,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/79",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 1,
        "definition_name" : "yarn_resourcemanager_rpc_latency",
        "host_name" : "eu-lamp-dev-xl-0018-hadoop-master",
        "id" : 79,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/80",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 6,
        "definition_name" : "yarn_resourcemanager_cpu",
        "host_name" : "eu-lamp-dev-xl-0018-hadoop-master",
        "id" : 80,
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/alerts/32",
      "Alert" : {
        "cluster_name" : "XXXXX",
        "definition_id" : 3,
        "definition_name" : "yarn_app_timeline_server_webui",
        "host_name" : "eu-lamp-dev-xl-0019-hadoop-sec-master",
        "id" : 32,
        "service_name" : "YARN"
      }
    }
  ],
  "components" : [
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/components/APP_TIMELINE_SERVER",
      "ServiceComponentInfo" : {
        "cluster_name" : "XXXXX",
        "component_name" : "APP_TIMELINE_SERVER",
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/components/NODEMANAGER",
      "ServiceComponentInfo" : {
        "cluster_name" : "XXXXX",
        "component_name" : "NODEMANAGER",
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/components/RESOURCEMANAGER",
      "ServiceComponentInfo" : {
        "cluster_name" : "XXXXX",
        "component_name" : "RESOURCEMANAGER",
        "service_name" : "YARN"
      }
    },
    {
      "href" : "http://XX.XX.XX.XX/api/v1/clusters/XXXXX/services/YARN/components/YARN_CLIENT",
      "ServiceComponentInfo" : {
        "cluster_name" : "XXXXX",
        "component_name" : "YARN_CLIENT",
        "service_name" : "YARN"
      }
    }
  ],
  "artifacts" : [ ]

I need to search any 2 or 3 patterns in above file e.g. UNKNOWN and CRITICAL,

I used the command $

awk '/UNKNOWN/ && /CRITICAL/ { print; }' <file_name> 

But it is not working, Please help

Hello saurau,

Seems you want lines which have strings either UNKNOWN or CRITICAL in single line. The method used by you will look for a line which has both strings in it, since there is no line like that in input file provided by you so result is correct which is NULL, use below and let me know if that helps you.

 awk '/UNKNOWN/ || /CRITICAL/'  Input_file
 

Output is as follows.

     "CRITICAL" : 7,
    "UNKNOWN" : 3,
 

Thanks,
R. Singh

Sorry for not asking exact question, I want AND operator so that if both exists then only i will get the output.

Both do not exist on any line, so the output is correct, perhaps the question is what constitutes a line?
Do you first want to capture the alert_summary hashmap and if both have numbers > 0 associated with them print the entire JSON text to a file?

awk '/UNKNOWN/ {x=$0} x!="" && /
CRITICAL/ { print x; print; exit }' <file_name>

---------- Post updated at 08:08 AM ---------- Previous update was at 07:51 AM ----------

awk '/UNKNOWN/ {x=$0} x!="" && /CRITICAL/ { print x; print; exit }' <file_name>