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