Unix Shell - how to not match a match?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2006 > how to not match a match?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to not match a match?
yusuf

2006-03-16, 5:53 pm

hi, I want to print out lines that do not match "field-power" in them.
I have gotten the expression which matches the line, but can't seem to
do the inverse. see:

awk '/(field\-power)/ { print $0 }'

Mr_Bill

2006-03-16, 5:53 pm

On Thu, 16 Mar 2006 09:51:01 -0800, yusuf wrote:

> hi, I want to print out lines that do not match "field-power" in them.
> I have gotten the expression which matches the line, but can't seem to
> do the inverse. see:
>
> awk '/(field\-power)/ { print $0 }'


grep -v "field-power" filename

Mr_Bill
yusuf

2006-03-16, 5:53 pm

Thnaks, that worked. Just for my knowledge, do you know how to do it in
AWK (or regex in general)?


Mr_Bill wrote:
> On Thu, 16 Mar 2006 09:51:01 -0800, yusuf wrote:
>
>
> grep -v "field-power" filename
>
> Mr_Bill


Hajo Ehlers

2006-03-16, 5:53 pm


awk ' /(field\-power)/ { print $0 }' # match for
awk ' ! /(field\-power)/ { print $0 }' # Does not match for

hth
Hajo

Mr_Bill

2006-03-16, 5:53 pm

On Thu, 16 Mar 2006 10:02:34 -0800, yusuf wrote:

> Thnaks, that worked. Just for my knowledge, do you know how to do it in
> AWK (or regex in general)?


gawk '{if($0 !~ /field-power/) print $0}' filename

"!~" isn't regex, it is one of gawk/awk's conditional operators.

Mr_Bill

Mr_Bill

2006-03-16, 5:53 pm

On Thu, 16 Mar 2006 10:15:40 -0800, Hajo Ehlers wrote:

>
> awk ' /(field\-power)/ { print $0 }' # match for
> awk ' ! /(field\-power)/ { print $0 }' # Does not match for
>
> hth
> Hajo



I like your's better! My Gawk and Bash allows

gawk '!/field-power/ {print}' filename

M_B
Ed Morton

2006-03-16, 5:53 pm

Mr_Bill wrote:
> On Thu, 16 Mar 2006 10:15:40 -0800, Hajo Ehlers wrote:
>
>
>
>
>
> I like your's better! My Gawk and Bash allows
>
> gawk '!/field-power/ {print}' filename
>
> M_B


awk '!/field-power/' filename

Ed.
Juha Laiho

2006-03-17, 5:55 pm

"yusuf" <yusufm@gmail.com> said:
>hi, I want to print out lines that do not match "field-power" in them.
>I have gotten the expression which matches the line, but can't seem to
>do the inverse. see:
>
>awk '/(field\-power)/ { print $0 }'


One more way; not elegant in this case, but usable in some:
awk '/field-power/ { next } { print $0; }'
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Ed Morton

2006-03-17, 5:55 pm

Juha Laiho wrote:
> "yusuf" <yusufm@gmail.com> said:
>
>
>
> One more way; not elegant in this case, but usable in some:
> awk '/field-power/ { next } { print $0; }'


awk '/field-power/ { next } 1'

Ed.
Chris F.A. Johnson

2006-03-17, 5:55 pm

On 2006-03-17, Ed Morton wrote:
> Juha Laiho wrote:
>
> awk '/field-power/ { next } 1'


FORE! (a.k.a. Golf alert)

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Ed Morton

2006-03-17, 5:55 pm

Chris F.A. Johnson wrote:
> On 2006-03-17, Ed Morton wrote:
>
>
>
> FORE! (a.k.a. Golf alert)
>


No, the golf solution would have the white-space removed:

awk '/field-power/{next}1'

Instead, the above is just instruction on an awk idiom.

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com