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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing a block of lines from a file, if that block does not contain two patterns using sed
# 1  
Old 12-21-2012
Linux 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.
Code:
................................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

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

the output iam getting is given below
Code:
 ................................server 1...............................
................................server 1...............................
running                     process 1
running                     process 2
running                     process 3
date : 12/12/2012
............................................................................
............................................................................
................................server 2...............................

The required output is
Code:
 ................................server 1...............................
running                     process 1
 running                     process 2
 running                     process 3
 date : 12/12/2012
 ............................................................................

Please let me know what am I doing wrong.Smilie

Last edited by Scrutinizer; 12-21-2012 at 09:32 AM.. Reason: icode to code tagss
# 2  
Old 12-21-2012
awk OK? try something like this:
Code:
awk 'p{p=p RS $0} /\.\.\.\./ {if(p~s && !(p~/stop/)){print p; p=x}else p=$0}' s="12.12.2012" file


Last edited by Scrutinizer; 12-21-2012 at 10:02 AM..
# 3  
Old 12-21-2012
Quote:
Originally Posted by Scrutinizer
awk OK? try something like this:
Code:
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.
# 4  
Old 12-21-2012
I don't understand, there is no "11.12.2012" in your sample, I get:
Code:
$ 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
$

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

# 5  
Old 12-21-2012
Is this what you want to do?

Code:
$ 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.

Last edited by Subbeh; 12-21-2012 at 10:44 AM.. Reason: forgot about the date part
# 6  
Old 12-21-2012
Perhaps you mean this:
Code:
$ 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
............................................................................
$

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 12-21-2012
I am not sure if this is correct:
Code:
sed -n '/^\.\.\.\./!H; /^\.\.\.\./{x; /stopped/d; /date.*13\/12\/2012/d; p;}' example

These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk/sed command to pull out matching lines from a block

Hi, In order to make our debugging easier in log files, I need this script. My log file will be structured like this : ------Invoking myfile -param:start_time=1371150900000 -param:end_time=1371151800000 for 06/14/2013 <multiple lines here> ..... - Step Sybase CDR Table.0 ended... (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

2. Shell Programming and Scripting

Removing a block of duplicate lines from a file

Hi all, I have a file with the data 1 abc 2 123 3 ; 4 rao 5 bell 6 ; 7 call 8 abc 9 123 10 ; 11 rao 12 bell 13 ; (10 Replies)
Discussion started by: raosr020
10 Replies

3. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

4. Shell Programming and Scripting

extracting block of lines from a file

consider the input file which i am dealing with looks like this.. #cat 11.sql create table abc ( . . . ) engine=Innodb ; . . etc . . . create table UsM ( blah blah blah ) engine=Innodb ; (5 Replies)
Discussion started by: vivek d r
5 Replies

5. UNIX for Dummies Questions & Answers

Printing the lines using search patterns

Hi all , i need an help here.!!!! i have a file that contains /etc/passwd files from some servers. i need a script which search for presence of a user in the servers. like if i give 51144 to the script. the should be o/p Please help on this..... (4 Replies)
Discussion started by: sudharson
4 Replies

6. Shell Programming and Scripting

Grep between block of lines in a file

Hi Friends, I have a file which has many of the statements like below ******** MAKING > noun1 < cg_all statements statements statements ********* MAKING > noun2 < cg_all statements statements statements ********* MAKING > noun3 < all statements statements statements I would... (3 Replies)
Discussion started by: ganga.dharan
3 Replies

7. Shell Programming and Scripting

Counting lines in each block file

hello im new here so i want to say hi everybody :) i have to write a script and im newbie :/ i hope that in this forum are many ppl who knows subject :) i have hundrets folders. in each folder is a file name trace.txt. each trace.txt has a lot of tracert's results separates with "-----" it... (6 Replies)
Discussion started by: michael8484
6 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

Replacing Block of lines in a text file

Dear All, Regards of the Day. I have a text file with some functions: Function1 { parameter 1 parameter 2 parameter 3 } end Function2 { parameter 1 parameter 2 parameter 3 } (1 Reply)
Discussion started by: ashisharora
1 Replies

10. Shell Programming and Scripting

Read block of lines from xml file

Hi I am new to this forum. I have few XML files and from each xml file I want to copy some specific 50 no of lines and copy them to some other file. how to do that? pls help.. (5 Replies)
Discussion started by: pritam1980
5 Replies
Login or Register to Ask a Question