get text between two special rows ?(awk or sed)?

Hello,

I've the follwing text:

gfdgfg
--------------------------------
dfgfdgdfg
fdgfdgfdgdgf
fdgf
------------------------------
f
g
gdgf
a constant string that i know
---------------------------------------------
data I want to have data I want to have
data I want to have data I want to have
....
data I want to have data I want to have
data I want to have data I want to have
----------------------------------------------
fdgfdg
fdgfdgd
fdgfdg
---------------------------------
dsfgfdgfdg
dsfgfdgfd
dfsg
-----------------------------------
sfdgfdgf
fdgfdg

I want to get the text between the rows ------------------------
Before this there is a known constant String.
I've tried with sed, but with no result. I've the problem that
there are mutiples textparts between the rows --------------------

Any ideas?

Thanks a lot,
Eric

awk '/start_pattern/,/end_pattern/ { print }' filename

Hy matrixmadhan,

is it with awk possible to create a pattern over 2 rows?

best regards, Eric

Something like this:

awk '/^---/{f=0}/constant/{getline;f=1;next}f' filename

Use nawk or /usr/xpg4/bin/awk on Solaris.

can you post sample input and output ?

Hy,

Input is like the my first post and output should be only the bold text.
or in ohter words

I know the string from the first line, so i think the startpattern have to find

Known_string\n

the end pattern have to find the next row with

But the row with

occurs multiple times in the text

Can I match this with awk or sed ?

Best regards, Eric

somewhat ugly, but......
nawk -f eric.awk myFile

eric.awk:

BEGIN {
  PAT="constant string"
}

$0 ~ PAT {f=1;print;next}
f && /^--*$/ {b=1;print;next}
b,/^--*/
{f=0;b=0}

Hm ..., perhaps I've missed something,
not elegant solution :):

awk '/^---/{c++}/constant/{c=0}/constant/,c==2' filename

good idea, radoulov - nice!

awk '{ if ( $0 ~ "constant string" ) { find=1; getline tmp; print  } if ( find && tmp !~ $0 ) { print tmp; last } }' filename

Modified a bit:

awk '/^---/{c++}/constant/{c=0}!c,c==2' c=1  filename

Thanks a lot to all,

I believe that awk seemed to be very powerful and complex.
So I've searched for further informations:
[Chapter 7] 7.3 Awk's Programming Model

When I've understood your examples, especialy from radoulov I will be able to solve my problem

Thanks and best regards,
Eric

Hy radoulov,

you script works very fine, but what does

!c,c==2

do?

thanks, Eric

In Awk:

Print the records from the one where c is not true (!c): c equals 0 (the records with your constant string, because of this code:

/constant/{c=0}

), to where c equals 2.

try this

sed -n '$1,$2p' <file-Name> where $1,$2 are row numbers..

Hi Matrix,
your first solution is also helpful for me.
Thanks
Piyush

Thanks Dear...!!!