For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2007 > How to .....









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 .....
Paul Bullack

2007-02-24, 3:57 am

Hello,

how to result only these lines which have NOT "-" as the 3rd field using
awk

data:
field1 - field3 field4 field5 LINE1
field1 field2 - field4 field5 LINE2
field1 field2 field3 field4 field5 LINE3
field1 field2 - field4 field5 LINE4
field1 field2 field3 field4 - LINE5


result:
field1 - field3 field4 field5 LINE1
field1 field2 field3 field4 field5 LINE3
field1 field2 field3 field4 - LINE5

Thanks Paul


Dan Foster

2007-02-24, 3:57 am

In article <eroq5l$g8f$01$1@news.t-online.com>, Paul Bullack <paul.bullack@t-online.de> wrote:
> Hello,
>
> how to result only these lines which have NOT "-" as the 3rd field using
> awk
>
> data:
> field1 - field3 field4 field5 LINE1
> field1 field2 - field4 field5 LINE2
> field1 field2 field3 field4 field5 LINE3
> field1 field2 - field4 field5 LINE4
> field1 field2 field3 field4 - LINE5


Hallo,

Assuming data is in a file named 'data':

$ awk '$3 !~ "-" { print $0 }' data

What this means is:

1) Read input from a file named data
2) If third field does not match a string named -
3) Then print the entire line (known as $0)
4) Otherwise it goes back to loop to process data again
5) If no more lines to read from the input source, quit

You don't have to read from a file. awk can receive its data through
stdin as well, if you prefer.

-Dan
loki harfagr

2007-02-24, 7:57 am

On Sat, 24 Feb 2007 02:08:53 -0600, Dan Foster wrote:

> In article <eroq5l$g8f$01$1@news.t-online.com>, Paul Bullack
> <paul.bullack@t-online.de> wrote:
>
> Hallo,
>
> Assuming data is in a file named 'data':
>
> $ awk '$3 !~ "-" { print $0 }' data
>
> What this means is:
>
> 1) Read input from a file named data
> 2) If third field does not match a string named - 3) Then print the
> entire line (known as $0) 4) Otherwise it goes back to loop to process
> data again 5) If no more lines to read from the input source, quit
>
> You don't have to read from a file. awk can receive its data through
> stdin as well, if you prefer.
>
> -Dan


Correct, and the "short" form of it is :
# awk '$3!~/^-$/' data
Janis Papanagnou

2007-02-25, 3:57 am

loki harfagr wrote:
> On Sat, 24 Feb 2007 02:08:53 -0600, Dan Foster wrote:
>
>
>
>
> Correct, and the "short" form of it is :
> # awk '$3!~/^-$/' data


Why use a regexp match if comparing to a string constant?

The "shorter" form of it is

awk '$3!="-"' data


Janis
Paul Bullack

2007-02-25, 3:57 am

Thank you all, bye ....

"Paul Bullack" <paul.bullack@t-online.de> schrieb im Newsbeitrag
news:eroq5l$g8f$01$1@news.t-online.com...
> Hello,
>
> how to result only these lines which have NOT "-" as the 3rd field using
> awk
>
> data:
> field1 - field3 field4 field5 LINE1
> field1 field2 - field4 field5 LINE2
> field1 field2 field3 field4 field5 LINE3
> field1 field2 - field4 field5 LINE4
> field1 field2 field3 field4 - LINE5
>
>
> result:
> field1 - field3 field4 field5 LINE1
> field1 field2 field3 field4 field5 LINE3
> field1 field2 field3 field4 - LINE5
>
> Thanks Paul
>



Alan Margino

2007-02-25, 6:57 pm

One dull day if ever some lost imp scratched :

> loki harfagr wrote:
>
> Why use a regexp match if comparing to a string constant?


Well, ahem, I'd say bad habits from me to use belt and suspenders ;-)

>
> The "shorter" form of it is
>
> awk '$3!="-"' data


Though, there was another possible reason why I'd propose the
regexp form, the OP didn't precise what OS+awk he'd use and I noticed
he posted with MSOE thus maybe he'll have to have a hell of a time with
quoting style.
I used to have a hell of a time with this when using
MKSTK or similar (hence the previously admitted "bad habits" ;D)

Anyway, you're prefectly right to say the "shorter" form uses "-"
now let's wait someone comes along with the "shortest" form ?-)

Oh well, it's Sunday again ...
loki harfagr

2007-02-25, 6:57 pm

On Sun, 25 Feb 2007 15:37:30 +0000, Alan Margino wrote:


Sorry about the dumb use of the dummy account, I was playing with the
latest new Pan client pre-release!

> Oh well, it's Sunday again ...


that's it!
Janis Papanagnou

2007-02-25, 6:57 pm

Alan Margino wrote:
> One dull day if ever some lost imp scratched :
>
>
>
>
> Well, ahem, I'd say bad habits from me to use belt and suspenders ;-)
>
>
>
>
> Though, there was another possible reason why I'd propose the
> regexp form, the OP didn't precise what OS+awk he'd use and I noticed
> he posted with MSOE thus maybe he'll have to have a hell of a time with
> quoting style.


On WinDOS I'd always put awk programs in a file and call it with -f option
to avoid the quoting misery.

....I know it's hard if your awk program consists of just 7 characters. :-/


> I used to have a hell of a time with this when using
> MKSTK or similar (hence the previously admitted "bad habits" ;D)
>
> Anyway, you're prefectly right to say the "shorter" form uses "-"
> now let's wait someone comes along with the "shortest" form ?-)


I wanted to use that superlative but was reluctant to do; leaves room for
other awk wizards/golfers. ;-)

Janis

>
> Oh well, it's Sunday again ...

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com