0

I want to only print server name as header and whatever sas service is not up inside it.if everything is up inside server it should not be printed.

File content-

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

Desired Output-

   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

i tried below which is giving me server header even if everything is up inside it.

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
William Pursell
  • 204,365
  • 48
  • 270
  • 300
DGaynar
  • 1
  • 1
  • 2

3 Answers3

0

Awk is more suitable for such cases:

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

The output:

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
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Thanks a lot. it does exactly what i wanted. can you please elaborate what it is doing. i know only basic of awk. – DGaynar Oct 13 '17 at 18:59
0

It may be laughable :-) but the following sed command seems to work for me:

sed -n '/^he2unix5/ {h}; /NOT up$/ {x;H;p;x;P;s/.*//;x}' sasserverstatus.log
Ken Schumack
  • 719
  • 4
  • 11
0

With sed :

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

Or to keep your look :

sed -n '/^he2unix5/{1bA;s/^/\n/;:A;s/$/\n/;h};/NOT up$/{x;G;p;s/.*//;x}' sasserverstatus.log
ctac_
  • 2,413
  • 2
  • 7
  • 17