For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2006 > variable range of input









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 variable range of input
felix

2006-03-24, 6:59 pm

Heya,

I have different files where I want to extract numbers of. Condition is
like that: if $2 matches, print out some fields. In $2 there are digits
which may change slightly, let's say target would be 12.99, but 13.01
and 12.97 are also valid. Until now I used the following code in a small
script:

$2 ~ /12.97|12.98|12.99|13.00|13.01/ { print $2, $8, $9}

Is there a way to define s.th. like a range in this case? Like "match
all values from 12.97 until 13.01"?

Christian
Janis Papanagnou

2006-03-24, 6:59 pm

felix wrote:
> Heya,
>
> I have different files where I want to extract numbers of. Condition is
> like that: if $2 matches, print out some fields. In $2 there are digits
> which may change slightly, let's say target would be 12.99, but 13.01
> and 12.97 are also valid. Until now I used the following code in a small
> script:
>
> $2 ~ /12.97|12.98|12.99|13.00|13.01/ { print $2, $8, $9}
>
> Is there a way to define s.th. like a range in this case? Like "match
> all values from 12.97 until 13.01"?


How about...

$2 >= 12.97 && $2 <= 13.01 { print $2, $8, $9}


Janis

>
> Christian

felix

2006-03-28, 6:56 pm


> $2 >= 12.97 && $2 <= 13.01 { print $2, $8, $9}
>

Hm, I tried it out, there is no output, even not an error...
With inputfile like
2 12.978 8 BB AA x y 123 5.8
I get no output with the above exact code. Also change of 12.978 to 13.0
doesn't produce output. With
$2 ~ /12.97/ { print $2 }
I get the correct number. Any idea?

Christian
Janis Papanagnou

2006-03-28, 6:56 pm

felix wrote:
>
> Hm, I tried it out, there is no output, even not an error...


How did you start it (file or command line)? Which platform (Unix or
Windows)? Which locale settings (C, POSIX, or anything else)?

> With inputfile like
> 2 12.978 8 BB AA x y 123 5.8
> I get no output with the above exact code.


On Unix...

$ echo "2 12.978 8 BB AA x y 123 5.8" |
awk '$2 >= 12.97 && $2 <= 13.01 { print $2, $8, $9 }'
12.978 123 5.8

You may try to change your settings to "LANG=C" or "LANG=POSIX".
What does "echo $LANG" show you? And what "set | grep LC_"?

> Also change of 12.978 to 13.0
> doesn't produce output. With
> $2 ~ /12.97/ { print $2 }
> I get the correct number.


Pattern matching compares against a pattern string, not a number.
(Try whether this program accepts the input "12x97" in field 2.)

> Any idea?


Since you reside in DE maybe a locale setting problem.

On WinDOS you will have to put the awk command in double quotes, or
put it in a file 'yourfile' to interpret it by "awk -f yourfile".

Janis
felix

2006-03-29, 7:56 am


> How did you start it (file or command line)? Which platform (Unix or
> Windows)? Which locale settings (C, POSIX, or anything else)?

I am using Linux Suse10 with the bash. I recently changed to Linux, so
forgive my little knowledge about settings...
As I am currently trying out several variants I put the code in a script
invoking it with
awk -f script1 file
> $ echo "2 12.978 8 BB AA x y 123 5.8" |
> awk '$2 >= 12.97 && $2 <= 13.01 { print $2, $8, $9 }'
> 12.978 123 5.8

no output;
when I do the same without range specification i.e.
$2 ~ /12.97/
the output is
12.978
> You may try to change your settings to "LANG=C" or "LANG=POSIX".
> What does "echo $LANG" show you? And what "set | grep LC_"?

LANG=de_DE.UTF-8, LC_ is not set. Is it possible to set more than one
LANG? I wouldn't like to miss UTF-8 encoding. Or do I need to change
other parameters as well?
> Pattern matching compares against a pattern string, not a number.

This is what into my mind. Do I need to tell awk that the file contains
numbers? Or
> (Try whether this program accepts the input "12x97" in field 2.)

with /12x97/
I get
12x97
> Since you reside in DE maybe a locale setting problem.

de_DE. ??

Thanks a lot for your comments. I highly appreciate if you could help me
further solving my problems!

Chris
felix

2006-03-30, 3:56 am


> Since you reside in DE maybe a locale setting problem.


This might be the problem. Changing decimal separator in the input
number from dot to comma gives the expected result. Is there a way to
within the shell or awk to work with dot decimal separator, without
changing the locale setting? Well, this might not be the right forum for
this question...

Thanks a lot for your help
Christian
Janis Papanagnou

2006-03-30, 6:56 pm

felix wrote:
>
> This might be the problem. Changing decimal separator in the input
> number from dot to comma gives the expected result. Is there a way to
> within the shell or awk to work with dot decimal separator, without
> changing the locale setting?


Whether it expects a dot resp. a comma is the effect of the locale
setting. You may set the locale only for the specific command call
as I've done in the examples that I sent you per e-mail. But you
have to decide whether you want awk to behave as for locale A or B,
both is not possible (AFAICT).

Janis

> Well, this might not be the right forum for
> this question...
>
> Thanks a lot for your help
> Christian

Sponsored Links







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

Copyright 2008 codecomments.com