Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed.

See below for the example data.

................................server 1...............................
running                     process 1
running                     process 2
running                     process 3
date : 12/12/2012
............................................................................
................................server 2...............................
running                     process 1
stopped                    process 2
running                     process 3
date : 13/12/2012
............................................................................

in the above mentioned file a block refers from
.........server................. to ........................
in that block i have to check, if there is no pattern "stop" and "13/12/2012" then i have to print that block.

this is what i have tried till now

sed -n -e '/./{H;/\.\.\.\./!d;}' -e 'x;/stop/ !{ /13.12.2012/ !{p;}}' example

the output iam getting is given below

 ................................server 1...............................
................................server 1...............................
running                     process 1
running                     process 2
running                     process 3
date : 12/12/2012
............................................................................
............................................................................
................................server 2...............................

The required output is

 ................................server 1...............................
running                     process 1
 running                     process 2
 running                     process 3
 date : 12/12/2012
 ............................................................................

Please let me know what am I doing wrong.:b:

awk OK? try something like this:

awk 'p{p=p RS $0} /\.\.\.\./ {if(p~s && !(p~/stop/)){print p; p=x}else p=$0}' s="12.12.2012" file

Hello Scrutinizer,

Thanks for the reply.
This awk statement will not work, if the date is different for example
if date is "11.12.2012".

I want to print the block, which does not has pattern "stop" and does not has pattern "13.12.2012" in the real time it will be the current date.

I don't understand, there is no "11.12.2012" in your sample, I get:

$ awk 'p{p=p RS $0} /\.\.\.\./ {if(p~s && !(p~/stop/)){print p; p=x}else {p=$0}}' s="12.12.2012" file
................................server 1...............................
running                     process 1
running                     process 2
running                     process 3
date : 12/12/2012
$
$ awk 'p{p=p RS $0} /\.\.\.\./ {if(p~s && !(p~/stop/)){print p; p=x}else {p=$0}}' s="13.12.2012" file
$

Is this what you want to do?

$ awk -v d=$(date +%Y/%m/%d) '{p=$0~/stopped/?s=1:p=p RS $0}NR%6==0{if(!s)print p;p=s=x}/date/{if($3==d)s=1}' file

................................server 1...............................
running                     process 1
running                     process 2
running                     process 3
date : 12/12/2012
............................................................................

This only works if every block has 6 lines though.

Perhaps you mean this:

$ awk 'p{p=p RS $0} /\.\.\.\./ {if(p){ if(p!~s && p!~/stop/)print p; p=x}else {p=$0}}' s="13.12.2012" file
................................server 1...............................
running                     process 1
running                     process 2
running                     process 3
date : 12/12/2012
............................................................................
$
1 Like

I am not sure if this is correct:

sed -n '/^\.\.\.\./!H; /^\.\.\.\./{x; /stopped/d; /date.*13\/12\/2012/d; p;}' example
2 Likes

Thanks a lot, scrutinizer and Made in germany.
Both of your commands worked, I analysed and understood, how the comands work.
Thanks a lot again:b:

@MadeInGermany : Good to see, you using Hold buffer and pattern buffer concept in sed.

It will be great if you or someone please explain on how it comes handy in the above case. Thanks in advance.