Need help - from awk, sed experts

Hi ,

I have a file as below.

Contents of the file are
--------------------
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd
aaaaaaaaaaa

I want to do the follwing

if exists "ccccccccccc"
get
"bbbbbbbbbbb"
"ccccccccccc"
"ddddddddddd"
fi

Can someone help me in doing this using awk/sed..

thanks
srini

Hard to do with sed, but this seems to work on HP-UX. The script needs one arg which string to find.

#! /usr/bin/ksh

sed -n -e ":l" -e "h;n;s/$1/$1/;t m" -e "b l" -e ":m" -e "H;n;H;g;p"
exit 0

Hi Perderabo,

will it work in Linux ..

can you please let me know , how to run this .

Assumig file name is abc.txt and the search string is "ccccccc"

is it correct

./script.sh abc.txt

Thanks
Srini

./script.ksh ccccccccc < abc.txt

EDIT:

On linux, you may not have /usr/bin/ksh. I think it should work with bash. Change first line to a shell you have
#! /bin/bash
or whatever.

Hi Perderabo,

It works fine .

But can you please explain me what exactly happening here ,

sed -n -e ":l" -e "h;n;s/$1/$1/;t m" -e "b l" -e ":m" -e "H;n;H;g;p"

and what if,

if I want to get 3 lines before the search string and one line after the search string .

i.e
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
kkkkkkkkkkk
llllllllllllllllllllllllll
tttttttttttttt
bbbbbbbbbbb
ccccccccccc
ddddddddddd
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
kkkkkkkkkkk
llllllllllllllllllllllllll
tttttttttttttt
bbbbbbbbbbb
ccccccccccc
ddddddddddd
aaaaaaaaaaa
aaaaaaaaaaa
kkkkkkkkkkk
llllllllllllllllllllllllll
tttttttttttttt
bbbbbbbbbbb
ccccccccccc
ddddddddddd
aaaaaaaaaaa

Thanks
srini