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: s/// not an address?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

sed: s/// not an address?

Réponse
 
LinkBack Outils de la discussion
Vieux 20/12/2007, 15h08   #1
Robert Latest
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sed: s/// not an address?

Hello,

I'm kind of puzzled. I'm trying to look through a file, and when something
matches, I'd like to put a certain portion of the line in sed's hold space.

OK, here's what I want. Below is a snippet of "ps afx" output, and I need
the last SCREEN process ID before some command I'm interested in (in this
case, slrn).

----------------------------------------

5710 ? \_ SCREEN
5711 pts/17 \_ /bin/bash
5841 pts/17 \_ /bin/bash
5842 pts/17 \_ slrn

----------------------------------------

Here's my script so far:

------------
#!/bin/sh
ps afx | sed -nr "s/^ *([0-9]+) \\?.*SCREEN/\\1/h;/$1/{g}"
------------

But sed won't let me use the 'h' command after s///, and it says "Unknown
option to s". But why can I use, for instance, the p command but not h?

I could have written this in 30 seconds using perl, but I'm currently in a
stubborn sed phase. I don't understand all this address/command thing.

robert
  Réponse avec citation
Vieux 20/12/2007, 16h40   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: s/// not an address?



On 12/20/2007 9:08 AM, Robert Latest wrote:
> Hello,
>
> I'm kind of puzzled. I'm trying to look through a file, and when something
> matches, I'd like to put a certain portion of the line in sed's hold space.
>
> OK, here's what I want. Below is a snippet of "ps afx" output, and I need
> the last SCREEN process ID before some command I'm interested in (in this
> case, slrn).
>
> ----------------------------------------
>
> 5710 ? \_ SCREEN
> 5711 pts/17 \_ /bin/bash
> 5841 pts/17 \_ /bin/bash
> 5842 pts/17 \_ slrn
>
> ----------------------------------------
>
> Here's my script so far:
>
> ------------
> #!/bin/sh
> ps afx | sed -nr "s/^ *([0-9]+) \\?.*SCREEN/\\1/h;/$1/{g}"
> ------------
>
> But sed won't let me use the 'h' command after s///, and it says "Unknown
> option to s". But why can I use, for instance, the p command but not h?
>
> I could have written this in 30 seconds using perl, but I'm currently in a
> stubborn sed phase. I don't understand all this address/command thing.
>


Take a deep breath and put it behind you. Don't try to use sed for anything
other than simple substitutions. If you don't want to or aren't allowed to use
perl, use awk or ruby or.... Assuming the ps afx output is as you posted above,
the awk script would be:

ps afx | awk '$NF=="SCREEN"{p=$1} $NF=="slrn"{print p}'

See, all nice and clear and sensible....

Ed.

  Réponse avec citation
Vieux 20/12/2007, 17h05   #3
Edward Rosten
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: s/// not an address?

On Dec 20, 8:08 am, Robert Latest <boblat...@yahoo.com> wrote:

> ps afx | sed -nr "s/^ *([0-9]+) \\?.*SCREEN/\\1/h;/$1/{g}"
> ------------
>
> But sed won't let me use the 'h' command after s///, and it says "Unknown
> option to s". But why can I use, for instance, the p command but not h?


The s command is of this form:

s/pattern/replacement/flags

Note the flags at the end which control the usage (for instance "g"
makes it global, so it works on all instances on the current line, not
just the first). In other words, sed thinks that your "h" command is a
flag controlling the "s" command, and "h" isn't a valid flag. Try
inserting a semicolon:

sed -nr "s/^ *([0-9]+) \\?.*SCREEN/\\1/;h;/$1/{g}"


> I could have written this in 30 seconds using perl, but I'm currently in a
> stubborn sed phase. I don't understand all this address/command thing.


Good for you, sed is rather fun. It's worth leaarning since it's very
useful for oneliners, etc.

-Ed

  Réponse avec citation
Vieux 21/12/2007, 09h39   #4
Robert Latest
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: s/// not an address?

Edward Rosten wrote:

> The s command is of this form:
>
> s/pattern/replacement/flags
>
> Note the flags at the end which control the usage (for instance "g"
> makes it global, so it works on all instances on the current line, not
> just the first). In other words, sed thinks that your "h" command is a
> flag controlling the "s" command, and "h" isn't a valid flag. Try
> inserting a semicolon:


Yes, but 'p' is allowed at that point, and 'p' is a command after all.

> sed -nr "s/^ *([0-9]+) \\?.*SCREEN/\\1/;h;/$1/{g}"


But if I put a ; between the address and the command, how does sed know that
that command belongs to the address?

> Good for you, sed is rather fun. It's worth leaarning since it's very
> useful for oneliners, etc.


Yes, I've grown rather fond of the "find | sed | sh" approach for large
batches of files.

robert
  Réponse avec citation
Vieux 21/12/2007, 12h55   #5
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: s/// not an address?

Robert Latest wrote:
> Hello,
>
> I'm kind of puzzled. I'm trying to look through a file, and when something
> matches, I'd like to put a certain portion of the line in sed's hold space.
>
> OK, here's what I want. Below is a snippet of "ps afx" output, and I need
> the last SCREEN process ID before some command I'm interested in (in this
> case, slrn).
>
> ----------------------------------------
>
> 5710 ? \_ SCREEN
> 5711 pts/17 \_ /bin/bash
> 5841 pts/17 \_ /bin/bash
> 5842 pts/17 \_ slrn
>
> ----------------------------------------
>
> Here's my script so far:
>
> ------------
> #!/bin/sh
> ps afx | sed -nr "s/^ *([0-9]+) \\?.*SCREEN/\\1/h;/$1/{g}"
> ------------
>
> But sed won't let me use the 'h' command after s///, and it says "Unknown
> option to s". But why can I use, for instance, the p command but not h?
>
> I could have written this in 30 seconds using perl, but I'm currently in a
> stubborn sed phase. I don't understand all this address/command thing.
>
> robert



s/regexp/replacement/h
is syntax error: h is an unknown option to the s command.

s/regexp/replacement/;h
syntax correct, but h is unconditionally run.

You maybe want

/regexp/{s/regexp/replacement/;h;}

or

!s/regexp/replacement/;tL1
h
:L1
/$1/{g;}

Yes, better use perl or awk here.


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 21/12/2007, 13h12   #6
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: s/// not an address?

On Fri, 21 Dec 2007 13:55:46 +0100, Michael Tosch wrote:
[...]
> /regexp/{s/regexp/replacement/;h;}


or

/regexp/{s//replacement/;h;}

> or
>
> !s/regexp/replacement/;tL1


! is only run a command if the address range does *not* match.
You can't use it to negate the meaning of the "s" commands wrt
t.

> h
> :L1
> /$1/{g;}

[...]

Instead:

s/regexp/replacement/; t2
:1
/$1/{g;}
d
:2
h
b1

--
Stephane
  Réponse avec citation
Vieux 21/12/2007, 14h53   #7
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: s/// not an address?

Stephane Chazelas wrote:
> On Fri, 21 Dec 2007 13:55:46 +0100, Michael Tosch wrote:
> [...]
>> /regexp/{s/regexp/replacement/;h;}

>
> or
>
> /regexp/{s//replacement/;h;}
>
>> or
>>
>> !s/regexp/replacement/;tL1

>
> ! is only run a command if the address range does *not* match.
> You can't use it to negate the meaning of the "s" commands wrt
> t.
>
>> h
>> :L1
>> /$1/{g;}

> [...]
>
> Instead:
>
> s/regexp/replacement/; t2
> :1
> /$1/{g;}
> d
> :2
> h
> b1
>


Then, at least,
one should omit the d (or n) command:

s/regexp/replacement/; t1
b2
:1
h
:2
/$1/{g;}


--
Michael Tosch @ hp : com
  Réponse avec citation
Vieux 24/12/2007, 09h59   #8
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sed: s/// not an address?

Try this :

sed -ne '
/SCREEN/{
s/ .*//;h;d
}
/slrn/{g;}
'




On Dec 20, 8:08 pm, Robert Latest <boblat...@yahoo.com> wrote:
> Hello,
>
> I'm kind of puzzled. I'm trying to look through a file, and when something
> matches, I'd like to put a certain portion of the line in sed's hold space.
>
> OK, here's what I want. Below is a snippet of "ps afx" output, and I need
> the last SCREEN process ID before some command I'm interested in (in this
> case, slrn).
>
> ----------------------------------------
>
> 5710 ? \_ SCREEN
> 5711 pts/17 \_ /bin/bash
> 5841 pts/17 \_ /bin/bash
> 5842 pts/17 \_ slrn
>
> ----------------------------------------

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


É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,27378 seconds with 16 queries