a simple question about grep
a simple question about grep
am 06.09.2007 22:22:20 von Jerry
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!
Re: a simple question about grep
am 06.09.2007 22:47:00 von Ed Morton
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.
Re: a simple question about grep
am 06.09.2007 22:47:31 von Janis Papanagnou
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!
>
Re: a simple question about grep
am 07.09.2007 04:03:49 von mik3l3374
On Sep 7, 4:22 am, 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!
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
Re: a simple question about grep
am 07.09.2007 06:46:17 von Vakayil Thobias
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 "^*" | grep -v "^*INDICATOR"
Regards,
Thobias Vakayil
Re: a simple question about grep
am 08.09.2007 11:51:15 von Geoff Clare
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
Re: a simple question about grep
am 08.09.2007 13:40:16 von Stephane CHAZELAS
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
Re: a simple question about grep
am 08.09.2007 15:13:15 von Ed Morton
Geoff Clare wrote:
> Ed Morton wrote:
>
>
>>awk '/^*/ && !/^*INDICATOR/' file
>
>
> awk '/^\*/ && !/^\*INDICATOR/' file
> (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.
Re: a simple question about grep
am 08.09.2007 15:53:16 von Stephane CHAZELAS
2007-09-08, 08:13(-05), Ed Morton:
> Geoff Clare wrote:
>> Ed Morton wrote:
>>
>>>awk '/^*/ && !/^*INDICATOR/' file
>>
>>
>> awk '/^\*/ && !/^\*INDICATOR/' file
>
>> (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/009695399/basedefs/xbd_c hap09.html#tag_09_04_03
--
Stéphane
Re: a simple question about grep
am 08.09.2007 19:23:59 von Ed Morton
Stephane CHAZELAS wrote:
> 2007-09-08, 08:13(-05), Ed Morton:
>
>>Geoff Clare wrote:
>>
>>>Ed Morton wrote:
>>>
>>>
>>>>awk '/^*/ && !/^*INDICATOR/' file
>>>
>>>
>>>awk '/^\*/ && !/^\*INDICATOR/' file
>>
>>
>>
>>>(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/009695399/basedefs/xbd_c hap09.html#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.
Re: a simple question about grep
am 08.09.2007 19:35:54 von Stephane CHAZELAS
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
Re: a simple question about grep
am 12.09.2007 06:43:53 von Rob S
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -