return the previous line

Hello friends ,

I am doing the following command, but it is not wise to all files.

for temp in `find ./CSV/ -name  "*.txt"`
do
          sed -n -e 'N; /*Main End/p' $temp
done

Its give me the correct output for some files , but not for all files.
I mean some files contains the string *Main End , But it wont return me any value . But In some files it contains the exact *Main End pattern , it returns me the previous line as usual.

Pls help..

I am using Solaris 6.0

I think you misunderstand /*Main End/ as a regex.

/Main End$/ = Main End at the end of a line
/.*Main End/ = one or more of any character before Main End occurs  could be
   in the middle or at the end

The sed syntax is fine, it is your search expression causing your probelm.

Thanks for the reply.
But I opened in vi editor . And when I looked with the vi option
set list. It seems to be both same .

Ok. can you Please suggest me a R.E here for search the below
starting with a* follwing Main End.

Thank you in advance..

I ve two files looks as follows
file1

*Comment Frame 641 at 666.667 us
000000,CB1F,0,0,0,0,1,0,1
*Main End

file 2

000000,0000,0,0,2,0,0,0,0
*Comment Frame 
5,7E3D,0,0,0,0,1,0,1
*Main End

But when I using the command for both file

sed -n -e 'N; /\*Main End/p' file1

it should return me the line before The pattern *Main End

But it wont

But in case of file2 its working ,

Please need help urgently..

Try:
sed -n -e '/\*Main End/{g;p;q;};h' file1

This returns only the line before the Main End line. Do you really need that Main End line as well? If so try this one:
sed -n -e '/\*Main End/{H;g;p;q;};h' file1

Thanks for the reply.
I am afraid, It wont return anything either of these..

I need only the previous line..

Excuse me? I just retested them on both of your data files. Both sed commands continue to work correctly for me on both of your files.

$ cat file1
*Comment Frame 641 at 666.667 us
000000,CB1F,0,0,0,0,1,0,1
*Main End
$ sed -n -e '/\*Main End/{H;g;p;q;};h' file1
000000,CB1F,0,0,0,0,1,0,1
*Main End
$ sed -n -e '/\*Main End/{g;p;q;};h' file1
000000,CB1F,0,0,0,0,1,0,1
$
$
$
$
$
$ cat file2
000000,0000,0,0,2,0,0,0,0
*Comment Frame
5,7E3D,0,0,0,0,1,0,1
*Main End
$ sed -n -e '/\*Main End/{H;g;p;q;};h' file2
5,7E3D,0,0,0,0,1,0,1
*Main End
$ sed -n -e '/\*Main End/{g;p;q;};h' file2
5,7E3D,0,0,0,0,1,0,1
$

Sorry but I am exactly copying your file and code to my shell. But I am really very upset why its not working with my system..

In this case

sed -n -e '/\*Main End/{H;g;p;q;};h' file1

or

sed -n -e '/\*Main End/{H;g;p;q;};h' file1

it only returns me

*Main End

Please help ..

Yeah You are absolutely right its working under Linux.
But In Sun OS it's not working..

Thank you ..

Don't know what to tell you. It should work with any sed. :confused:

Anyway you can use awk to bypass the problem:

 /usr/xpg4/bin/awk '/\*Main End/{print x};{x=$0}' ${temp}

Oh genious .... I wish I could have this line before some day back.

Why are you using /usr/xpg4/bin/ infront of awk is it necessary ?
Could you Please explain me how are doing this magic ?

Any way thanks.
I love to be here..

The "other" awk is outdated.

Nice to help you.