Awking!! Printing decimal output is struck


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awking!! Printing decimal output is struck
# 1  
Old 05-28-2008
Awking!! Printing decimal output is struck

Hi friends,

I have a small problem with AWK. I am not able to print decimal values! Smilie below is my code:

#! /bin/awk -f
awk BEGIN{printf("%d",123)}; -> This prints the integer properly.

x=111
awk BEGIN{printf("%d",x)}; -> This doesnt print! Smilie

Please help me solve this. It is printing 0 as output.
Thanks,
Divya
# 2  
Old 05-28-2008
Your didn't initialized the variable x within the awk instructions. Try:

Code:
 awk 'BEGIN{x=111; printf "%d", x}'

# 3  
Old 05-28-2008
Hi,

If the value of x changes at the run time ,

u can use awk with -v option

awk -v val=$x 'BEGIN{printf("%d",val)}'

Thanks
Penchal
# 4  
Old 05-28-2008
Quote:
Originally Posted by ripat
Your didn't initialized the variable x within the awk instructions. Try:

Code:
 awk 'BEGIN{x=111; printf "%d", x}'

Thanku very much, you are helpful Smilie
# 5  
Old 05-28-2008
Quote:
Originally Posted by penchal_boddu
Hi,

If the value of x changes at the run time ,

u can use awk with -v option

awk -v val=$x 'BEGIN{printf("%d",val)}'

Thanks
Penchal
Thanks Penchal. Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Shell Programming and Scripting

Awking custom output

i have data that can look like this: echo "Master_Item_Service_is_down=0_njava_lang_NoClassDefFoundError=0_njava_lang_OutOfMemoryError=1_nemxCommonAppInitialization__Error_while_initializing=0_nINFO__Stopping_Coyote_HTTP_1_1_on_http_8080=7_nThe_file_or_directory_is_corrupted_and_unreadable=0_n" ... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Struck at shell command

Can someone help me to get two different outputs mentioned in code tags. Input: user=alexander registeredwebsite=yahoo.com user=james registeredwebsite=gmail.com registeredwebsite=fb.com registeredwebsite=google.com user=kelly registeredwebsite=gmail.com registeredwebsite=fb.com... (5 Replies)
Discussion started by: buzzme
5 Replies

4. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

5. Shell Programming and Scripting

Printing with decimal places from last 4 digits

I have input file like below, 201424|9999|OSS|622010|RGT|00378228764 201424|8888|OM|587079|RGT|00284329675 201424|7777|OM|587076|RGT|00128671024 201424|6666|OM|581528|RGT|00113552084 Output should be like below, should add decimal (.) from last 4 digits. ... (2 Replies)
Discussion started by: vinothsekark
2 Replies

6. Shell Programming and Scripting

Facing problem while printing accurate decimal value

Hi, I have a file tp.txt having below data CE2DD,N,5055,16.12.2013,3.114,12195.89,MVR,003388,Web::00000005055,Web Payment and i am using below code to print the values for var_amt_pay in `awk -F',' '{ arr += $6} END {for (i in arr) {print i "," arr } }' tp.txt` do ... (2 Replies)
Discussion started by: ranabhavish
2 Replies

7. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

8. UNIX for Dummies Questions & Answers

accuracy of output - decimal points

Is there a way when using awk to specify the number of decimal points needed for the output? (2 Replies)
Discussion started by: cosmologist
2 Replies

9. Shell Programming and Scripting

Struck with Typeset Command

Can someone explain whats this below command will do exactly.. typeset tmpFile1 tmpDir1 rc fileName fullName bc typeset tmpFile1 tmpFile2 rc fileSuffix uef typeset origFile bc fullName tmpDir1 remoteCmd inFile newInFile typeset num fn curDate fileSuffix ucFileName Since i'm new to... (1 Reply)
Discussion started by: help_scr_seeker
1 Replies

10. Shell Programming and Scripting

trimm up the decimal places in output

I have a perl script that reports the avg time of a application call and the total number of calls. This works fine, however I would like to trim the number of decimal places reported from 12 to like 3 and I don't know how. Any suggestions? Here is what I use to get the avg time... for $eRef (... (2 Replies)
Discussion started by: theninja
2 Replies
Login or Register to Ask a Question