Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk comparison

Status
Not open for further replies.

shaoji

Programmer
Mar 22, 2004
10
US
Hi,
I have a file as follows:

2|
10|
11|
12|
13|
14|
15|
16d|
2|
3|
31d||
4|
5|
6|
60d|
7|
8|
9|

if I run:

awk 'BEGIN{FS=OFS="|"}{if ($1>a ) {print $1 }; a=$1}' ss

I get

2
10
11
12
13
14
15
16d
2
3

So it means 2>16d>15>,,,>10>2.

and if I run

awk 'BEGIN{FS=OFS="|"}{if ($1>=1 && $1<=5 ) { $1 = "pp" } else if ($1>=6 && $1<=10 ) {$1 = "ss"} print $1}' ss

I got

pp
ss
11
12
13
14
15
pp
pp
pp
pp
pp
pp
ss
60d
ss
ss
ss

It means awk treats 15d as between 1 and 5, 31d between 1 and 5, but does not treat 60d between 6 and 16.

Could someone explain this?

Shaoji

 
and how about that:
awk 'BEGIN{FS=OFS="|"}{if ($1+0>=1 && $1+0<=5 ) { $1 = "pp" } else if ($1+0>=6 && $1+0<=10 ) {$1 = "ss"} print $1}'

NOTE: all the comparisons are string/ascii comparisons UNLESS the operands are converted to numbers.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Vlad,
I know how to solve this problem. I just don't know how the awk makes comparison. The first example shows, in awk
2>16d>10>2, how could this happen? The second example, why awk treats 16d and 60d differently?
Thanks again.
Shaoji
 
Awk variables can behave like strings or numbers depending on the context. When awk compares a number to a string the number is converted to a string before performing a lexical comparision.

The lexical sequence of your example data would be...
[tt]
1
15d
16
31d
5
6
60d[/tt]
 
The first example shows, in awk
2>16d>10>2, how could this happen?

the comparison is done as strings based on ascii codes and the above is correct: '2' is greater than '16d' (i.e. '2' > '1').
And '16d' is greater than '10' (because '6' > '0'). Why '10' > '2' - I don't know - have to look at your code.

The second example, why awk treats 16d and 60d differently?
What do you mean by 'differently'? I the code I posted - they are treated the same (is you convert them to ints).

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Vlad. You are right. I think the awk's comparison is the following:

If both are numbers, it compares as numbers (not as in "sort" command), so 10>2. If at least one of them is a string, then it comapre as strings (as in "sort" commmand.)
so 16d>100. It can explain 2>16d>100>5.

The second example is my misunderstanding.

Thanks.

Shaoji
 
Also thanks to Vgor. You are correct and helpful. In awk, it can happen as follows:

7
8
10
10d
2
3
7

Shaoji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top