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 > Sed line deletion
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Sed line deletion

Réponse
 
LinkBack Outils de la discussion
Vieux 02/11/2006, 02h47   #1
Greg Schafer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sed line deletion

Hi

Using GNU sed, how can I delete the line immediately _above_ the regexp?
Deleting the line below the regexp is no problem eg:

sed '/regexp/{n;d}' file

I cannot figure this one out.. but surely it's possible? I'm looking for a
"sed only" solution.

Thanks
Greg
  Réponse avec citation
Vieux 02/11/2006, 13h15   #2
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed line deletion

Greg Schafer wrote:
>
> Hi
>
> Using GNU sed, how can I delete the line immediately _above_ the regexp?


Here is the awk way to do it...

$ awk 'NR>1 && !/pattern/ {print s} {s=$0} END {print s}'

> Deleting the line below the regexp is no problem eg:
>
> sed '/regexp/{n;d}' file
>
> I cannot figure this one out.. but surely it's possible? I'm looking for a
> "sed only" solution.


I am wondering why you prefer a cryptic sed solution to a clear and
extensible awk solution. I am no sed expert but this might do it...

$ sed -n '1h;1!{/pattern/h;/pattern/!{x};${x}}'

(There may be a way to avoid duplication of the pattern; I don't
bother.)

Janis

> Thanks
> Greg


  Réponse avec citation
Vieux 02/11/2006, 17h51   #3
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed line deletion

Janis wrote:
> Greg Schafer wrote:
>> Hi
>>
>> Using GNU sed, how can I delete the line immediately _above_ the regexp?

>
> Here is the awk way to do it...
>
> $ awk 'NR>1 && !/pattern/ {print s} {s=$0} END {print s}'
>
>> Deleting the line below the regexp is no problem eg:
>>
>> sed '/regexp/{n;d}' file


Should be {n;d;} for standard sed and previous GNU sed.

>>
>> I cannot figure this one out.. but surely it's possible? I'm looking for a
>> "sed only" solution.

>
> I am wondering why you prefer a cryptic sed solution to a clear and
> extensible awk solution. I am no sed expert but this might do it...
>
> $ sed -n '1h;1!{/pattern/h;/pattern/!{x};${x}}'
>
> (There may be a way to avoid duplication of the pattern; I don't
> bother.)
>


Sure, there is.
But, compared to awk, a brain-teaser.

sed '1h;1d;/pattern/{h;$p;d;};x;$p;$x'

Standard sed is picky about the {} command groups;
they must be on separate lines or multiple -e args:

sed -e '1h;1d' -e '/pattern/{h;$p;d;}' -e 'x;$p;$x'

--
Michael Tosch @ hp : com
  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 07h08.


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07716 seconds with 11 queries