sed multiple multi line blocks of text containing pattern

Hi,

I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id.

The files are large at several Gb.

My example file is :

$> cat test
Date1;stuff;sessionid1;stuff;restofline1
Date2;stuff;sessionid1;stuff;restofline2
 Something1;stuff
Something2;stuff
Date3;stuff;sessionid2;stuff;restofline3
Date4;stuff;sessionid2;stuff;restofline4
Date5;stuff;sessionid1;stuff;restofline5
Date6;stuff;sessionid2;stuff;restofline6

my required output would be :

Date1;stuff;sessionid1;stuff;restofline1
Date2;stuff;sessionid1;stuff;restofline2
Something1;stuff
Something2;stuff
Date5;stuff;sessionid1;stuff;restofline5

I am totally unsure how to use the N command to process multiple lines between say regex "^Date.*;sessionid1;" and "^Date" but I think thats the direction I need to be going.

I'm using ksh on Solaris

Any help would be very much appreciated.

Here is an awk code:

/usr/xpg4/bin/awk '(/^Date/ && /sessionid1/) || !/^Date/' file

Yoda, your answer is not correct; it'll match !/^Date/ in the entire file!
It is necessary to have a kind of state; awk and a state variable is appropriate.

awk '/^Date/ {s=0} /^Date2;.*;sessionid1;/ {s=1} s' test

nawk or /usr/xpg4/bin/awk.
/bin/awk needs (this time with the OPs input)

awk '/^Date/ {s=0} /^Date.*;sessionid1;/ {s=1} s!=0' test
1 Like

Thanks, thats great ! Very neat.