LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 03-25-2008, 02:51 PM   #1
moutaye
LQ Newbie
 
Registered: Aug 2005
Posts: 28

Rep: Reputation: 15
awk/nawk returning decimal values?


Hi

Running a specific nawk statement over a 17m lines files returns the following:

/bin/nawk: not enough args in .....
input record number 1,25955e+06, file test.1
source line number 1

I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out.

I tried to pass the OTMF variable in the nawk statement to "%.8d" but nothing helped.

Any awk/nawk expert woudl be able to figure this out?

Thanks for your help guys
 
Old 03-25-2008, 03:22 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Maybe it is a typo, but the variable is OFMT. According to the AWK manual:
Quote:
According to the POSIX standard, awk’s behavior is undefined if OFMT contains anything but a floating-point conversion specification.
It is a good practice to define OFMT as "%.0f" to print numbers as integers. If the number has some decimal digits, it results in a rounding effect. I hope this will help.
 
Old 03-25-2008, 03:31 PM   #3
moutaye
LQ Newbie
 
Registered: Aug 2005
Posts: 28

Original Poster
Rep: Reputation: 15
thanks for the reply colucix

i actually meant OFMT

i tried your suggestion as follows:

/bin/nawk -F'|' '{OFMT="%.0f"; printf $1;printf "|"; printf $1;printf "|"; printf $2;printf "|"; printf $3;printf "|"; printf $3;printf "|"; printf $4;printf..........

I still get the float type value returned not the integer...

Any other idea?

Thanks for this
 
Old 03-25-2008, 04:30 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Tried gawk?
Code:
echo "" |awk '{printf"%d\n", 12595594224142}' 
12595594224142
And what's with all the printf-statements?

Code:
awk -F'|' '{printf "%d | %s | %s | %s | %s | %s |\n", NR, $1, $2, $3, $4, $5}' file


Cheers,
Tink
 
Old 03-25-2008, 04:57 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Two notes: 1. the variable OFMT controls the output of the print statement, which internally uses the C function sprintf to convert numbers to strings. If you use the printf statement, you can specify the format as in
Code:
printf "%.0f",$1   # or simply
printf "%d",$1
2. The format specified by the OFMT variable affects only the printing of numbers. But if you try to print an input field as in
Code:
print $1
the output is not affected by OFMT even if $1 represents a numeric value, since variables and fields are considered strings in awk, unless the context forces them to be treated as numbers. A workaround to force awk to consider variables and fields as numbers is to add 0, for example the statement
Code:
print $1+0
force awk to consider $1 as a number, due to the presence of an arithmetic operator and in this case it is affected by the format specified by OFMT.
 
Old 03-25-2008, 05:42 PM   #6
moutaye
LQ Newbie
 
Registered: Aug 2005
Posts: 28

Original Poster
Rep: Reputation: 15
thanks for the help on this guys

i might not have been clear but i'm not trying to convert float values or fields within the file to decimal. i'm trying to convert the actual line number output of the incriminated line reported by default by awk as a float if it exceeds 6 digits.

hence i'm actually not sure if OMFT can actually help as coculix outlined...

any other ideas?
 
Old 03-25-2008, 06:16 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Did you see my gawk example above? That's like outputting a line-number.
Maybe you've found an nawk limitation?

Actually I just checked: nawk will print those
long integers just fine using printf "%d"


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk / nawk question - hostname dazdaz Programming 4 03-07-2008 04:50 PM
How to share variable values between shell and awk? realos Programming 1 12-16-2006 11:15 PM
MySQL Returning very strange Values in PHP Script benrose111488 Programming 2 10-21-2004 12:26 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 11:05 PM
Add values using awk Helene Programming 4 04-21-2004 07:30 PM


All times are GMT -5. The time now is 03:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration