Compares not working past the 4th char

There probably is a simple explanation, and maybe I haven�t searched enough for it, or I've looked at this so long I can't see my own mistake, but here�s my issue.

My code is such

#!/bin/ksh 
awk ' 
{if ($1 == $2) {print $1,$2} }
' file1.txt 

File1.txt is this

000EEB2A 000EEB2A
000EEB2A 000EEB21
000EEB2A 000EEB22
000EEB2A 000EEB23
000EEB2A 000EEB24
000EEB2D 000EEB25
000EEB2D 000EEB2D
000EEBD4 000EEB2D
000EEBD4 000EEBD4
000EEC1D 000EEBD4

The results I�d thought I would get is

000EEB2A 000EEB2A
000EEB2D 000EEB2D
000EEBD4 000EEBD4

But I get this

000EEB2A 000EEB2A
000EEB2A 000EEB21
000EEB2A 000EEB22
000EEB2A 000EEB23
000EEB2A 000EEB24
000EEB2D 000EEB25
000EEB2D 000EEB2D
000EEBD4 000EEB2D
000EEBD4 000EEBD4
000EEC1D 000EEBD4

Here is the interesting thing to me. The first four characters in both fields are the same. If I modify one line to make the first four characters different, then it will not print that line. If I change the == equal to a != not equal I will only get a line that has the first four characters different. It seems anything past the first four is not considered.

This is correct. I see the desired results

tsthbs1:/tmp> cat a
#!/bin/ksh
 
#!/bin/ksh
awk '
{if ($1 == $2) {print $1,$2} }
' file
tsthbs1:/tmp> a
000EEB2A 000EEB2A
000EEB2D 000EEB2D
000EEBD4 000EEBD4
tsthbs1:/tmp>

Well at least I'm not crazy. But it may be a bug in my system. I'm running this on an IBM Mainframe in Unix System Services under zOS 2.2. Thanks

maybe is the format of the file. Try:

dos2ux < file.txt | awk '$1==$2 {print $1,$2}'

or

awk '{sub("\r$", "")} $1==$2 {print $1,$2}' file.txt

please provide the output of: cat -vet file using code tags.
Most likely you have ^M at the end of your lines.
If so, dos2unix file to convert.

Additional characters in one field should eliminate matches, not create more. Please post the output of od -tx1c File1.txt .

Perhaps the number recognition is broken?
Then a cast to a string would help

if ($1"" == $2"")
1 Like

So I guess the USS under ZOS 2.2 does things a bit different. Here is the answer from IBM

The issue here lies in the input
strings. awk's strings get converted to numbers and numbers to strings,
if the context of the program demands it. A string is converted to a
number by interpreting any numeric prefix of the string as numerals. For instance 37Jeff would be converted to 37. Hence, in your input files,
all the fields have a prefix of 000 which means that they are all
converted to 0 and are thus equal to awk.

To force a number to a string, you may prefix any numeric prefix with
the empty string "".

$ awk ' ""$1 == ""$2 { print $1,$2 }' awktest.txt

This worked for me for what I'm doing, and cured my headache.

Thanks to all you replied, it is appreciated

1 Like

Thanks for posting the "solution" (or explanation), and respect! to MadeInGermany for his educated guess!