|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Can I use ex to find a pattern and NOT print that line and the
previous x number of lines? thanks, tony |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
tony wrote:
> Can I use ex to find a pattern and NOT print that line and the > previous x number of lines? I doubt it and even if there was, ex would be a poor choice of tool for the job. Here's an awk solution: awk -v x="3" ' BEGIN{ ARGV[ARGC++] = ARGV[1] } NR==FNR { if ($0 ~ /pattern/) for (i=FNR-x; i<=FNR; i++) skip[i]; next } !(FNR in skip)' file If that isn't what you want, post a followup. Ed. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Thu, 09 Nov 2006 10:12:59 -0400, tony wrote:
> Can I use ex to find a pattern and NOT print that line and the > previous x number of lines? > > thanks, > tony Sure. For the particulr case of x=4 ex - filename <<! /pattern/-4;//d 1,$p ! Broken down /pattern/-4 4 lines before pattern ; start here // to the last pattern searched for d delete 1,$ lines 1 to end p print |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 09 Nov 2006 16:23:16 GMT, Icarus Sparry <usenet@icarus.freeuk.com>
wrote: >On Thu, 09 Nov 2006 10:12:59 -0400, tony wrote: > >> Can I use ex to find a pattern and NOT print that line and the >> previous x number of lines? >> >> thanks, >> tony > >Sure. For the particulr case of x=4 > >ex - filename <<! >/pattern/-4;//d >1,$p >! > >Broken down >/pattern/-4 4 lines before pattern >; start here >// to the last pattern searched for >d delete >1,$ lines 1 to end >p print Thanks. Can I do that on one line like.... ex - filename <<!/pattern/-4;//d1,$p |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Icarus Sparry wrote:
> On Thu, 09 Nov 2006 10:12:59 -0400, tony wrote: > > >>Can I use ex to find a pattern and NOT print that line and the >>previous x number of lines? >> >>thanks, >>tony > > > Sure. For the particulr case of x=4 > > ex - filename <<! > /pattern/-4;//d > 1,$p > ! > > Broken down > /pattern/-4 4 lines before pattern > ; start here > // to the last pattern searched for > d delete > 1,$ lines 1 to end > p print That doesn't work for me (bash on cygwin): $ cat file 1 2 3 4 5 6 pattern 7 8 9 $ cat tst.ex ex - "$1" <<! /pattern/-2;//d 1,$p ! $ ./tst.ex file $ ex --version VIM - Vi IMproved 6.4 (2005 Oct 15, compiled Oct 21 2005 13:43:01) On your system, does it handle cases where "pattern" appears on 2 consecutive lines. and or on multiple lines throughout the file? Regards, Ed. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Thu, 09 Nov 2006 12:33:30 -0400, tony <bigtoehere@hotmail.com>
wrote: >On 09 Nov 2006 16:23:16 GMT, Icarus Sparry <usenet@icarus.freeuk.com> >wrote: > >>On Thu, 09 Nov 2006 10:12:59 -0400, tony wrote: >> >>> Can I use ex to find a pattern and NOT print that line and the >>> previous x number of lines? >>> >>> thanks, >>> tony >> >>Sure. For the particulr case of x=4 >> >>ex - filename <<! >>/pattern/-4;//d >>1,$p >>! >> >>Broken down >>/pattern/-4 4 lines before pattern >>; start here >>// to the last pattern searched for >>d delete >>1,$ lines 1 to end >>p print > >Thanks. Can I do that on one line like.... > >ex - filename <<!/pattern/-4;//d1,$p I ran this from a file & found one problem. My file contains ^L giving the folllowing error. ==>ex ex.tst < myTexrFile "ex.tst" 4 lines, 50 characters ex: 0602-020 ^L is not a known command character. () Any way around that? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Ed Morton wrote:
> That doesn't work for me (bash on cygwin): > > $ cat file > 1 > 2 > 3 > 4 > 5 > 6 pattern > 7 > 8 > 9 > $ cat tst.ex > ex - "$1" <<! > /pattern/-2;//d > 1,$p bash interpolates shell variables in here documents, did you try this? 1,\$p > ! > $ ./tst.ex file > > $ ex --version > VIM - Vi IMproved 6.4 (2005 Oct 15, compiled Oct 21 2005 13:43:01) > > On your system, does it handle cases where "pattern" appears on 2 > consecutive lines. and or on multiple lines throughout the file? > > Regards, > > Ed. -- Posted via a free Usenet account from http://www.teranews.com |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
dt wrote:
> Ed Morton wrote: > >>That doesn't work for me (bash on cygwin): >> >>$ cat file >>1 >>2 >>3 >>4 >>5 >>6 pattern >>7 >>8 >>9 >>$ cat tst.ex >>ex - "$1" <<! >>/pattern/-2;//d >>1,$p > > > bash interpolates shell variables in here documents, Doh! I read "1,$" and "p", missing the alternative interpretation as "1," and "$p". Thanks. OK, now I have: $ cat tst.ex ex - "$1" <<'!' /pattern/-2;//d 1,$p ! $ ./tst.ex file 1 2 3 7 8 9 so it pretty much works but I get a few spurious newlines. If I add a second pattern to the file, however: $ cat file 1 2 3 4 pattern 5 6 7 pattern 8 9 $ ./tst.ex file 1 5 6 7 pattern 8 9 then it doesn't work. Ed. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Ed Morton wrote:
> dt wrote: >> Ed Morton wrote: >> >>>That doesn't work for me (bash on cygwin): >>> >>>$ cat file >>>1 >>>2 >>>3 >>>4 >>>5 >>>6 pattern >>>7 >>>8 >>>9 >>>$ cat tst.ex >>>ex - "$1" <<! >>>/pattern/-2;//d >>>1,$p >> >> >> bash interpolates shell variables in here documents, > > Doh! I read "1,$" and "p", missing the alternative interpretation as > "1," and "$p". Thanks. OK, now I have: > > $ cat tst.ex > ex - "$1" <<'!' > /pattern/-2;//d > 1,$p uh, surely you mean, 1,\$p > ! > $ ./tst.ex file > 1 > > 2 > > 3 > > 7 > > 8 > > 9 > > so it pretty much works but I get a few spurious newlines. If I add a > second pattern to the file, however: > > $ cat file > 1 > 2 > 3 > 4 pattern > 5 > 6 > 7 pattern > 8 > 9 > $ ./tst.ex file > 1 > > 5 > > 6 > > 7 pattern > > 8 > > 9 > > then it doesn't work. > > Ed. -- Posted via a free Usenet account from http://www.teranews.com |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
dt wrote:
> Ed Morton wrote: >> $ cat tst.ex >> ex - "$1" <<'!' >> /pattern/-2;//d >> 1,$p > > uh, surely you mean, never mind, my bad > 1,\$p > >> ! I just looked in bash's man page, I didn't realize if the word closing the here document is quoted, no parameter interpolation occurs inside the here document son of a gun, I've never used that feature before :-) -- Posted via a free Usenet account from http://www.teranews.com |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On Thu, 09 Nov 2006 12:33:30 -0400, tony
<bigtoehere@hotmail.com> wrote: > > Thanks. Can I do that on one line like.... > > ex - filename <<!/pattern/-4;//d1,$p In some shells (bash and zsh), you have "here strings" with <<<. -- Many are cold, but few are frozen. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
On Thu, 09 Nov 2006 15:42:09 -0800, dt <dt.news@mailnull.com> wrote:
>Ed Morton wrote: >> That doesn't work for me (bash on cygwin): >> >> $ cat file >> 1 >> 2 >> 3 >> 4 >> 5 >> 6 pattern >> 7 >> 8 >> 9 >> $ cat tst.ex >> ex - "$1" <<! >> /pattern/-2;//d >> 1,$p > >bash interpolates shell variables in here documents, >did you try this? > > 1,\$p > I'm ksh and have this working now but I'd still like to do the opposite. I want to print everything but. Any idea if I can do that? >> ! >> $ ./tst.ex file >> >> $ ex --version >> VIM - Vi IMproved 6.4 (2005 Oct 15, compiled Oct 21 2005 13:43:01) >> >> On your system, does it handle cases where "pattern" appears on 2 >> consecutive lines. and or on multiple lines throughout the file? >> >> Regards, >> >> Ed. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
Bill Marcum wrote:
> On Thu, 09 Nov 2006 12:33:30 -0400, tony > <bigtoehere@hotmail.com> wrote: > >>Thanks. Can I do that on one line like.... >> >>ex - filename <<!/pattern/-4;//d1,$p > > In some shells (bash and zsh), you have "here strings" with <<<. Add ksh93 to the list. Janis |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
2006-11-10, 19:47(+01), Janis Papanagnou:
> Bill Marcum wrote: >> On Thu, 09 Nov 2006 12:33:30 -0400, tony >> <bigtoehere@hotmail.com> wrote: >> >>>Thanks. Can I do that on one line like.... >>> >>>ex - filename <<!/pattern/-4;//d1,$p >> >> In some shells (bash and zsh), you have "here strings" with <<<. > > Add ksh93 to the list. [...] And rc, where it all comes from. I beleive the heritage chain is: rc -> zsh -> ksh -> bash though it might be rc -> zsh -> ksh -> bash ksh acknowledges the feature comes from zsh in its RELEASE file. (of course, es and akanga both based on rc have it as well). -- Stéphane |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
tony wrote:
> On Thu, 09 Nov 2006 15:42:09 -0800, dt <dt.news@mailnull.com> wrote: > > >>Ed Morton wrote: >> >>>That doesn't work for me (bash on cygwin): >>> >>>$ cat file >>>1 >>>2 >>>3 >>>4 >>>5 >>>6 pattern >>>7 >>>8 >>>9 >>>$ cat tst.ex >>>ex - "$1" <<! >>>/pattern/-2;//d >>>1,$p >> >>bash interpolates shell variables in here documents, >>did you try this? >> >> 1,\$p >> > > > I'm ksh and have this working now Are you sure? That seems unlikely without significant rework unless you can guarantee the pattern only appears once in the file. but I'd still like to do the > opposite. I want to print everything but. Any idea if I can do that? Your original question was to print everything but the pattern and N preceeding lines. If you're saying you now want to print a pattern plus N lines of context, use GNU grep with the appropriate context argument(s). Regards, Ed. |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
On Fri, 10 Nov 2006 18:14:53 -0600, Ed Morton <morton@lsupcaemnt.com>
wrote: >tony wrote: > >> On Thu, 09 Nov 2006 15:42:09 -0800, dt <dt.news@mailnull.com> wrote: >> >> >>>Ed Morton wrote: >>> >>>>That doesn't work for me (bash on cygwin): >>>> >>>>$ cat file >>>>1 >>>>2 >>>>3 >>>>4 >>>>5 >>>>6 pattern >>>>7 >>>>8 >>>>9 >>>>$ cat tst.ex >>>>ex - "$1" <<! >>>>/pattern/-2;//d >>>>1,$p >>> >>>bash interpolates shell variables in here documents, >>>did you try this? >>> >>> 1,\$p >>> >> >> >> I'm ksh and have this working now > >Are you sure? That seems unlikely without significant rework unless you >can guarantee the pattern only appears once in the file. > I was just talking about syntax, thanks. > but I'd still like to do the >> opposite. I want to print everything but. Any idea if I can do that? > >Your original question was to print everything but the pattern and N >preceeding lines. If you're saying you now want to print a pattern plus >N lines of context, use GNU grep with the appropriate context argument(s). > >Regards, > > Ed. Yes, that correct. I want to print everything but the pattern and N preceeding lines. Sorry if my question was confusing. |
|
![]() |
| Outils de la discussion | |
|
|