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

ex?

Réponse
 
LinkBack Outils de la discussion
Vieux 09/11/2006, 14h12   #1
tony
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ex?

Can I use ex to find a pattern and NOT print that line and the
previous x number of lines?

thanks,
tony
  Réponse avec citation
Vieux 09/11/2006, 15h41   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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.
  Réponse avec citation
Vieux 09/11/2006, 16h23   #3
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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
  Réponse avec citation
Vieux 09/11/2006, 16h33   #4
tony
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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
  Réponse avec citation
Vieux 09/11/2006, 16h44   #5
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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.
  Réponse avec citation
Vieux 09/11/2006, 17h01   #6
tony
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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?

  Réponse avec citation
Vieux 09/11/2006, 23h42   #7
dt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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

  Réponse avec citation
Vieux 10/11/2006, 00h06   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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.
  Réponse avec citation
Vieux 10/11/2006, 00h26   #9
dt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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

  Réponse avec citation
Vieux 10/11/2006, 00h33   #10
dt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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

  Réponse avec citation
Vieux 10/11/2006, 08h29   #11
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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.
  Réponse avec citation
Vieux 10/11/2006, 15h22   #12
tony
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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.


  Réponse avec citation
Vieux 10/11/2006, 18h47   #13
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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
  Réponse avec citation
Vieux 10/11/2006, 22h45   #14
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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
  Réponse avec citation
Vieux 11/11/2006, 00h14   #15
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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.
  Réponse avec citation
Vieux 14/11/2006, 20h23   #16
tony
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ex?

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.
  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,19817 seconds with 24 queries