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

a simple question about grep

Réponse
 
LinkBack Outils de la discussion
Vieux 06/09/2007, 21h22   #1
Jerry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut a simple question about grep

Hi,

I have a text file whose content looks like below:

*INDICATOR name1 zip1
geoid gender location
*INDICATOR name2 zip2
*geoid gender location
INDICATOR name3 zip3
*district court


I want to pick up all lines starting with "*" but no "INDICATOR"
followed.

So for the example above, I want to pick up the following 2 lines:

(the 3rd line) *geoid gender location
(the last line) *district court

How to use regular expression to achieve this goal?

Thank you!

  Réponse avec citation
Vieux 06/09/2007, 21h47   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

Jerry wrote:
> Hi,
>
> I have a text file whose content looks like below:
>
> *INDICATOR name1 zip1
> geoid gender location
> *INDICATOR name2 zip2
> *geoid gender location
> INDICATOR name3 zip3
> *district court
>
>
> I want to pick up all lines starting with "*" but no "INDICATOR"
> followed.
>
> So for the example above, I want to pick up the following 2 lines:
>
> (the 3rd line) *geoid gender location
> (the last line) *district court
>
> How to use regular expression to achieve this goal?


I find awks syntax much more intuitive than grep for anything beyond
simple REs:

awk '/^*/ && !/^*INDICATOR/' file

Ed.
  Réponse avec citation
Vieux 06/09/2007, 21h47   #3
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

Jerry wrote:
> Hi,
>
> I have a text file whose content looks like below:
>
> *INDICATOR name1 zip1
> geoid gender location
> *INDICATOR name2 zip2
> *geoid gender location
> INDICATOR name3 zip3
> *district court
>
>
> I want to pick up all lines starting with "*" but no "INDICATOR"
> followed.
>
> So for the example above, I want to pick up the following 2 lines:
>
> (the 3rd line) *geoid gender location
> (the last line) *district court
>
> How to use regular expression to achieve this goal?


One possibility is to use the inverse pattern...

grep -E -v '^([^*]|\*INDICATOR)'


Janis

>
> Thank you!
>

  Réponse avec citation
Vieux 07/09/2007, 03h03   #4
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

On Sep 7, 4:22 am, Jerry <gree...@gmail.com> wrote:
> Hi,
>
> I have a text file whose content looks like below:
>
> *INDICATOR name1 zip1
> geoid gender location
> *INDICATOR name2 zip2
> *geoid gender location
> INDICATOR name3 zip3
> *district court
>
> I want to pick up all lines starting with "*" but no "INDICATOR"
> followed.
>
> So for the example above, I want to pick up the following 2 lines:
>
> (the 3rd line) *geoid gender location
> (the last line) *district court
>
> How to use regular expression to achieve this goal?
>
> Thank you!


if you are able to use Python: here's a more readable version
#!/usr/bin/python
for line in open("file"):
if line.startswith("*") and not line.startswith("*INDICATOR"):
print line

  Réponse avec citation
Vieux 07/09/2007, 05h46   #5
Vakayil Thobias
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

Jerry wrote:
> Hi,
>
> I have a text file whose content looks like below:
>
> *INDICATOR name1 zip1
> geoid gender location
> *INDICATOR name2 zip2
> *geoid gender location
> INDICATOR name3 zip3
> *district court
>
>
> I want to pick up all lines starting with "*" but no "INDICATOR"
> followed.
>
> So for the example above, I want to pick up the following 2 lines:
>
> (the 3rd line) *geoid gender location
> (the last line) *district court
>
> How to use regular expression to achieve this goal?
>
> Thank you!
>


grep "^*" <filename> | grep -v "^*INDICATOR"

Regards,
Thobias Vakayil
  Réponse avec citation
Vieux 08/09/2007, 10h51   #6
Geoff Clare
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

Ed Morton wrote:

> awk '/^*/ && !/^*INDICATOR/' file


awk '/^\*/ && !/^\*INDICATOR/' file

or

sed -n -e '/^*INDICATOR/d' -e '/^*/p' file

(The need to quote the '*' in awk is a difference between BREs and EREs.)

--
Geoff Clare <netnews@gclare.org.uk>
  Réponse avec citation
Vieux 08/09/2007, 12h40   #7
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

2007-09-8, 10:51(+01), Geoff Clare:
> Ed Morton wrote:
>
>> awk '/^*/ && !/^*INDICATOR/' file

>
> awk '/^\*/ && !/^\*INDICATOR/' file
>
> or
>
> sed -n -e '/^*INDICATOR/d' -e '/^*/p' file


Or:

sed '/^*/!d;/^*INDICATOR/d'
>
> (The need to quote the '*' in awk is a difference between BREs and EREs.)


also:

grep -ve '^*INDICATOR' -e '^[^*]' -e '^$'

--
Stéphane
  Réponse avec citation
Vieux 08/09/2007, 14h13   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

Geoff Clare wrote:
> Ed Morton wrote:
>
>
>>awk '/^*/ && !/^*INDICATOR/' file

>
>
> awk '/^\*/ && !/^\*INDICATOR/' file

<snip>
> (The need to quote the '*' in awk is a difference between BREs and EREs.)
>


Could you post sample input where the backslash is necessary in this case?

Ed.
  Réponse avec citation
Vieux 08/09/2007, 14h53   #9
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

2007-09-08, 08:13(-05), Ed Morton:
> Geoff Clare wrote:
>> Ed Morton wrote:
>>
>>>awk '/^*/ && !/^*INDICATOR/' file

>>
>>
>> awk '/^\*/ && !/^\*INDICATOR/' file

> <snip>
>> (The need to quote the '*' in awk is a difference between BREs and EREs.)
>>

>
> Could you post sample input where the backslash is necessary in this case?

[...]

$ awk '/^*/'
awk: line 1: regular expression compile failed (syntax error ^* or ^+)

Check:
http://www.opengroup.org/onlinepubs/...l#tag_09_04_03

--
Stéphane
  Réponse avec citation
Vieux 08/09/2007, 18h23   #10
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

Stephane CHAZELAS wrote:
> 2007-09-08, 08:13(-05), Ed Morton:
>
>>Geoff Clare wrote:
>>
>>>Ed Morton wrote:
>>>
>>>
>>>>awk '/^*/ && !/^*INDICATOR/' file
>>>
>>>
>>>awk '/^\*/ && !/^\*INDICATOR/' file

>>
>><snip>
>>
>>>(The need to quote the '*' in awk is a difference between BREs and EREs.)
>>>

>>
>>Could you post sample input where the backslash is necessary in this case?

>
> [...]
>
> $ awk '/^*/'
> awk: line 1: regular expression compile failed (syntax error ^* or ^+)
>
> Check:
> http://www.opengroup.org/onlinepubs/...l#tag_09_04_03
>


Interesting. I tried it on a sample of awks:

$ echo 1 | awk '/^*/'
1
$ echo 1 | oawk '/^*/'
1
$ echo 1 | nawk '/^*/'
1
$ echo 1 | /usr/xpg4/bin/awk '/^*/'
/usr/xpg4/bin/awk: /^*/: ?, *, +, or { } not preceded by valid regular
expression Context is:
>>> /^*/ <<<

$ echo 1 | gawk '/^*/'
$ echo 1 | gawk --posix '/^*/'
gawk: fatal: Invalid preceding regular expression: /^*/

and it looks like only gawk without --posix actually does what I
expected it to do. Thanks for pointing that out. Next time I won't be so
lazy....

What awk did you use to get the output you did above?

Ed.
  Réponse avec citation
Vieux 08/09/2007, 18h35   #11
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

2007-09-08, 12:23(-05), Ed Morton:
[...]
>> $ awk '/^*/'
>> awk: line 1: regular expression compile failed (syntax error ^* or ^+)

[...]
> $ echo 1 | awk '/^*/'
> 1
> $ echo 1 | oawk '/^*/'
> 1
> $ echo 1 | nawk '/^*/'
> 1
> $ echo 1 | /usr/xpg4/bin/awk '/^*/'
> /usr/xpg4/bin/awk: /^*/: ?, *, +, or { } not preceded by valid regular
> expression Context is:
> >>> /^*/ <<<

> $ echo 1 | gawk '/^*/'
> $ echo 1 | gawk --posix '/^*/'
> gawk: fatal: Invalid preceding regular expression: /^*/
>
> and it looks like only gawk without --posix actually does what I
> expected it to do. Thanks for pointing that out. Next time I won't be so
> lazy....
>
> What awk did you use to get the output you did above?

[...]

That's mawk 1.3.3 on debian. mawk is claimed to be POSIX
compliant, smaller and faster than gawk (I've not verified it
myself), that's why you sometimes find it as the default awk on
some Linux distributions instead of gawk.

--
Stéphane
  Réponse avec citation
Vieux 12/09/2007, 05h43   #12
Rob S
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: a simple question about grep

Stephane CHAZELAS wrote:
> 2007-09-08, 12:23(-05), Ed Morton:
> [...]
>>> $ awk '/^*/'
>>> awk: line 1: regular expression compile failed (syntax error ^* or ^+)

> [...]
>> $ echo 1 | awk '/^*/'
>> 1
>> $ echo 1 | oawk '/^*/'
>> 1
>> $ echo 1 | nawk '/^*/'
>> 1
>> $ echo 1 | /usr/xpg4/bin/awk '/^*/'
>> /usr/xpg4/bin/awk: /^*/: ?, *, +, or { } not preceded by valid regular
>> expression Context is:
>>>>> /^*/ <<<

>> $ echo 1 | gawk '/^*/'
>> $ echo 1 | gawk --posix '/^*/'
>> gawk: fatal: Invalid preceding regular expression: /^*/
>>
>> and it looks like only gawk without --posix actually does what I
>> expected it to do. Thanks for pointing that out. Next time I won't be so
>> lazy....
>>
>> What awk did you use to get the output you did above?

> [...]
>
> That's mawk 1.3.3 on debian. mawk is claimed to be POSIX
> compliant, smaller and faster than gawk (I've not verified it
> myself), that's why you sometimes find it as the default awk on
> some Linux distributions instead of gawk.
>


Prompted me to check, Ubuntu Gutsy Tribe 5 (7.10 beta) mawk 1.3.3

For a Linux distro that is trying for the mainstream desktop and hopes
of breaking into the business desktop, I would have expected a more
mainstream version of awk.

--

Rob
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
http://www.aspir8or.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -



This is Unix and Netware country, on a quiet night you can hear NT Reboot.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  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 00h32.


É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,20382 seconds with 20 queries