|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|