awk output discrepancy


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk output discrepancy
# 1  
Old 01-20-2014
Question awk output discrepancy

I noticed a discrepancy while running AWK on different platforms/versions:

SunOS
Code:
$ echo "78" | awk '{ printf "%c\n", $0 }'
N
$ awk 'BEGIN{ printf "%c\n", "78" }' /dev/null
N

Linux / HP-UX
Code:
$ echo "78" | awk '{ printf "%c\n", $0 }'
N
$ awk 'BEGIN{ printf "%c\n", "78" }' /dev/null
7

Can somebody explain why the output is 7 instead of N?
# 2  
Old 01-20-2014
Hi Yoda, it is numerical versus string input for the printf command. Compare:
Code:
awk 'BEGIN{ printf "%c\n", "78" }'

to
Code:
awk 'BEGIN{ printf "%c\n", 78 }'

or
Code:
echo "78" | awk '{ printf "%c\n", $0 }'

vs.
Code:
echo "78" | awk '{ printf "%c\n", $0"" }'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-20-2014
I'd expect 7, since it's a string in that case. It seems it has to do with when it's converted between a string and int. I've several awk implementations installed locally, oawk is from heirloom tools which should be close to SunOS's.

Code:
$ try() { for awk in gawk mawk oawk bawk; do printf '%s: ' "$awk"; $awk "$@"; done; }
$ try 'BEGIN{ printf "%c\n", "78" }' /dev/null
gawk: 7
mawk: 7
oawk: N
bawk: 7

So what's oawk doing? Well, looks like it's using it as an int ALL THE TIME. I haven't the oawk code around anymore to dig any deeper. Perhaps it has its own printf? I've only known %c to be useful for casting an int to char. But in that first bit, it is internally made an int, unless we tack a null string to it then we're back to getting 7's.

Code:
for awk in gawk mawk oawk bawk; do printf '%s: ' "$awk"; echo 78 | $awk '{ printf "%c\n", $0 "" }'; done;
gawk: 7
mawk: 7
oawk: N
bawk: 7

This User Gave Thanks to neutronscott For This Post:
# 4  
Old 01-20-2014
Thanks Scrutinizer, that explains it!

I added a 0 to convert and it worked:
Code:
$ cat file
796f6461

Code:
awk '
        {
                for ( i = 1; i <= length($0); i += 2 )
                {
                        hex = sprintf ( "0x%s", substr ( $0, i, 2 ) )
                        dec = sprintf ( "%d", strtonum ( hex ) )
                        sym = sprintf ( "%c", dec )
                        s = s ? s sym : sym
                }
        }
        END {
                print s
        }
' file
1119

Code:
awk '
        {
                for ( i = 1; i <= length($0); i += 2 )
                {
                        hex = sprintf ( "0x%s", substr ( $0, i, 2 ) )
                        dec = sprintf ( "%d", strtonum ( hex ) )
                        dec = dec + 0
                        sym = sprintf ( "%c", dec )
                        s = s ? s sym : sym
                }
        }
        END {
                print s
        }
' file
yoda

# 5  
Old 01-20-2014
oh I took the wrong approach with that answer. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Memory and cache access time discrepancy

#include<stdio.h> #include<stdlib.h> #include<sys/time.h> #include<time.h> #include "rdtsc.h" #define SIZE 4*64*1024 int main() { unsigned long long a,b; int arr={0}; int i; register int r; a=rdtsc(); r=arr; b=rdtsc(); printf("1st element Access Cycles = %llu\n",b-a); (2 Replies)
Discussion started by: Vaibhavs1985
2 Replies

2. OS X (Apple)

RAM Usage discrepancy

Hey there! I'm a new user here who registered because I couldn't get these kind of questions answered in the place I directly com from. :o I've found a discrepancy in total RAM used and I can't figure out why it is. My only guess is there are some RAM used by some stuff impossible to identify,... (2 Replies)
Discussion started by: dasx
2 Replies

3. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

4. Shell Programming and Scripting

Text Highlighting Discrepancy, Caused by SED?

> reverse=`tput rev` > revert=`tput sgr0` > var="This is some note." > echo $var This is some note. > var2="This is ${reverse}some${revert} note." > echo $var2 This is some note. > var3=$(echo $var | sed 's/some/${reverse}some${revert}/g') > echo $var3 This is... (3 Replies)
Discussion started by: adamreiswig
3 Replies

5. Solaris

Sun X4170 M2 RAM discrepancy

Hi all, We are seeing an odd problem on one of our new servers. It seems to be reporting 4MB less RAM than is installed: # prtconf | grep Mem Memory size: 32764 Megabytes Our other servers for example shows none missing: # prtconf | grep Mem Memory size: 32768 Megabytes Both... (5 Replies)
Discussion started by: wmd
5 Replies

6. Shell Programming and Scripting

Discrepancy in finding the top memory consuming processes

When I run 'top' command,I see the following Memory: 32G real, 12G free, 96G swap free Though it shows as 12G free,I am not able to account for processes that consume the rest 20G. In my understanding some process should be consuming atleast 15-16 G but I am not able to find them. Is... (1 Reply)
Discussion started by: prasperl
1 Replies

7. Shell Programming and Scripting

AWK: Discrepancy in numeric output

During a file-system cleanup I noticed a strange behavior of awk (HP-UX 11iv3 / IA64). When summing up the size of files in one directory it gives different numbers when using print as opposed to printf: find . -type f -name '*.dmp.Z' -mtime +35 -exec ls -l {} \+ | \ awk 'BEGIN{ OFMT="%f" } {... (1 Reply)
Discussion started by: pludi
1 Replies

8. Solaris

Discrepancy in /var/adm/messages

A fresh installation was done in a machine of name arenal1 on Feb 26. But in the /var/adm/messages of the machine, there are messages having date Nov 12. Also the name of the machine is also different in the messages. Can anyone please tell me the reason for this discrepancy. (15 Replies)
Discussion started by: sundar3350
15 Replies

9. UNIX for Dummies Questions & Answers

How do I get past an HTML::entities discrepancy on an RPM?

I have an RPM that I am trying to install and it keeps coming back with: I know I could kill the bird by throwing a "yum install *perl*" at it, but this seems like hurling a skyscraper at an ant... any better suggestions? (2 Replies)
Discussion started by: jjinno
2 Replies

10. UNIX for Dummies Questions & Answers

NIS login discrepancy

I have a query in relation to a couple of machines I have set up. We will call them machine SUN and HPUX and they are running those operating systems respectively. The SUN machine is acting as an NIS server and the HPUX machine as an NIS client. Now the HPUX machine also has a an auto mounted file... (11 Replies)
Discussion started by: Henrik
11 Replies
Login or Register to Ask a Question