Unix Shell - How to catch a block which contains some keywords with sed

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > How to catch a block which contains some keywords with sed





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to catch a block which contains some keywords with sed
samuel

2007-05-23, 7:16 am

Hi ,

Now I need to analyze a file which is composed of several blocks ,
which is defined as below :

Start
<content>
<content>
.......
<content>
End



And I need to catch/print all the blocks which contains several
specific keywords such "cpu" "dma" in the content part.

Does any know to do this in sed or in other ways ?

Thanks in advance !

Samuel

Stephane CHAZELAS

2007-05-23, 7:16 am

2007-05-22, 23:42(-07), samuel:
[...]
> Now I need to analyze a file which is composed of several blocks ,
> which is defined as below :
>
> Start
> <content>
> <content>
> ......
> <content>
> End
>
>
>
> And I need to catch/print all the blocks which contains several
> specific keywords such "cpu" "dma" in the content part.
>
> Does any know to do this in sed or in other ways ?

[...]

It's not easy with sed and you may have size restrictions.
Easier with perl:

perl -0777 -ne'print for grep /cpu|dma/, (/^Start\n(.*?)^End$/mgs)'

Or

perl -0777 -ne'print for grep /cpu|dma/, (/^Start\n.*?^End\n/mgs)'

If you want to include the Start/End tags.

--
Stéphane
Ed Morton

2007-05-23, 7:16 am

samuel wrote:

> Hi ,
>
> Now I need to analyze a file which is composed of several blocks ,
> which is defined as below :
>
> Start
> <content>
> <content>
> ......
> <content>
> End
>
>
>
> And I need to catch/print all the blocks which contains several
> specific keywords such "cpu" "dma" in the content part.
>
> Does any know to do this in sed or in other ways ?


Do you mean contains one of the keywords or contains some combination of
the keywords or contains all of the keywords or something else?

> Thanks in advance !
>
> Samuel
>


Try this to start with:

awk '
/Start/ { inBlock=1; block="" }
inBlock { block=block $0 ORS; if (/cpu|dma/) inBlock=2 }
/End/ { if (inBlock==2) printf "%s",block; inBlock=0 }
' file

then clarify your requirements if necessary.

Ed.
sil

2007-05-24, 1:18 pm

On May 23, 2:42 am, samuel <samuelz...@gmail.com> wrote:
> Hi ,
>
> Now I need to analyze a file which is composed of several blocks ,
> which is defined as below :
>
> Start
> <content>
> <content>
> ......
> <content>
> End
>
> And I need to catch/print all the blocks which contains several
> specific keywords such "cpu" "dma" in the content part.
>
> Does any know to do this in sed or in other ways ?
>
> Thanks in advance !
>
> Samuel



Does sed '/cpu/!d; /dma/!d' filename not work for this...

samuel

2007-05-31, 1:21 am

On May 25, 12:10 am, sil <dsphunx...@gmail.com> wrote:
> On May 23, 2:42 am, samuel <samuelz...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
> Does sed '/cpu/!d; /dma/!d' filename not work for this...- Hide quoted text -
>
> - Show quoted text -


Thanks Ed and all,
perl -0777 -ne'print for grep /cpu|dma/, (/^Start\n(.*?)^End$/mgs)'
Or
perl -0777 -ne'print for grep /cpu|dma/, (/^Start\n.*?^End\n/mgs)'


did works in this case.

Rgds,

Samuel

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com