如何在特定图案中找到图案并打印它们

时间:2017-10-13 18:42:15

标签: unix awk sed text-processing

我想只打印服务器名称作为标题,并且任何sas服务都不在其中。如果服务器内部的所有内容都不应该打印。

文件内容 -

he2unix554 Server: 

SAS servers status:

SAS OLAP Server 1 is UP

SAS Object Spawner 1 is UP

SAS CONNECT Spawner 1 is NOT up

SAS Environment Manager Agent is NOT up

he2unix555 Server: 

SAS servers status:

SAS OLAP Server 1 is UP

SAS Object Spawner 1 is UP

SAS CONNECT Spawner 1 is NOT up

SAS Environment Manager Agent is UP

he2unix556 server:

SAS servers status:

SAS OLAP Server 1 is UP

SAS Object Spawner 1 is UP

SAS CONNECT Spawner 1 is UP

SAS Environment Manager Agent is NOT up

he2unix557 Server

SAS servers status:

SAS OLAP Server 1 is NOT up

SAS Object Spawner 1 is UP

SAS CONNECT Spawner 1 is UP

SAS Environment Manager Agent is UP

he2unix558 Server

SAS Web Infrastructure Data Server is UP

SAS OLAP Server 1 is UP

SAS Object Spawner 1 is UP

SAS DIP Job Runner 1 is UP

SAS CONNECT Spawner 1 is UP

SAS Cache Locator Service ins_41415 is NOT up

SAS Environment Manager Agent is NOT up

he2unix559 Server

SAS OLAP Server 1 is UP

SAS Object Spawner 1 is UP

SAS CONNECT Spawner 1 is UP

SAS Environment Manager Agent is UP

所需的输出 -

   he2unix554 Server: 

    SAS CONNECT Spawner 1 is NOT up

    SAS Environment Manager Agent is NOT up

    he2unix555 Server: 

    SAS CONNECT Spawner 1 is NOT up

    he2unix556 server:

    SAS Environment Manager Agent is NOT up

    he2unix557 Server:

    SAS OLAP Server 1 is NOT up

    he2unix558 Server:

    SAS Cache Locator Service ins_41415 is NOT up

我尝试下面即使内部的所有内容也都给了我服务器标题。

sed -n' / ^ he2unix5 / {p}; / NOT up $ / {p}' sasserverstatus.log

he2unix554 Server: 

SAS CONNECT Spawner 1 is NOT up

SAS Environment Manager Agent is NOT up

he2unix555 Server: 

SAS CONNECT Spawner 1 is NOT up

he2unix556 server:

SAS Environment Manager Agent is NOT up

he2unix557 Server:

SAS OLAP Server 1 is NOT up

he2unix558 Server:

SAS Cache Locator Service ins_41415 is NOT up

**he2unix559 Server:**   #don't want this in output

3 个答案:

答案 0 :(得分:0)

Awk 更适合此类情况:

awk '/^he2unix/{ sn=$0; f=1; sas=0 }f && /NOT up/{ if(!sas++) print sn; print $0 }' file

输出:

he2unix554 Server: 
SAS CONNECT Spawner 1 is NOT up
SAS Environment Manager Agent is NOT up
he2unix555 Server: 
SAS CONNECT Spawner 1 is NOT up
he2unix556 server:
SAS Environment Manager Agent is NOT up
he2unix557 Server
SAS OLAP Server 1 is NOT up
he2unix558 Server
SAS Cache Locator Service ins_41415 is NOT up
SAS Environment Manager Agent is NOT up

答案 1 :(得分:0)

这可能是可笑的:-)但是以下sed命令似乎对我有用:

sed -n '/^he2unix5/ {h}; /NOT up$/ {x;H;p;x;P;s/.*//;x}' sasserverstatus.log

答案 2 :(得分:0)

使用sed:

sed -n '/^he2unix5/h;/NOT up$/{x;G;s/^\n//;p;s/.*//;x}' sasserverstatus.log

或者保持你的外观:

sed -n '/^he2unix5/{1bA;s/^/\n/;:A;s/$/\n/;h};/NOT up$/{x;G;p;s/.*//;x}' sasserverstatus.log