PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > How to catch a block which contains some keywords with sed
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to catch a block which contains some keywords with sed

Réponse
 
LinkBack Outils de la discussion
Vieux 23/05/2007, 07h42   #1
samuel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to catch a block which contains some keywords with sed

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

  Réponse avec citation
Vieux 23/05/2007, 10h42   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to catch a block which contains some keywords with sed

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
  Réponse avec citation
Vieux 23/05/2007, 12h44   #3
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to catch a block which contains some keywords with sed

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.
  Réponse avec citation
Vieux 24/05/2007, 17h10   #4
sil
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to catch a block which contains some keywords with sed

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...

  Réponse avec citation
Vieux 31/05/2007, 07h14   #5
samuel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to catch a block which contains some keywords with sed

On May 25, 12:10 am, sil <dsphunx...@gmail.com> wrote:
> 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...- 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13696 seconds with 13 queries