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