LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2007, 08:12 AM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
Multiplying data column in awk - can rounding be suppressed?


Hi.

I have a large XYZ data file. Columns X and Y contain x-positions and y-positions - both are in metres. Column Z holds data values.

I have to pass this data file in to a code - but firstly the x,y positions have to be converted to kilometres.

My problem is that awk rounds the values upon multiplication by 0.001 - leading to a loss of data accuracy:

Code:
awk -v OFS="\t" '{print $1*0.001, $2*0.001, $3}' datafile
So for example x position 123456 metres is converted to 123.5 metres or 123.46 metres say.....Is there any switch to suppress this rounding in awk?


Thanks.
 
Old 02-28-2007, 08:39 AM   #2
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
formatted printing takes care of it:

Code:
awk '{printf "%8.3f  %8.3f  %5.2f\n", $1*0.001, $2*0.001, $3}' datafile
 
Old 02-28-2007, 08:56 AM   #3
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
use printf instead of print :
Quote:
awk -v OFS="\t" '{printf "%.3f %.3f %.3f", $1*0.001, $2*0.001, $3}' datafile
where %.3f gives a floating number with 3 digits after the decimal point.

Last edited by berbae; 02-28-2007 at 09:04 AM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 08:07 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 12:57 PM
awk column printing schneidz Programming 7 09-29-2005 07:14 AM
'Last Post' Column showing incorrect data Shade LQ Suggestions & Feedback 1 04-20-2005 02:31 AM
Finding the name and data type of a column in SQL Travis86 Programming 11 08-15-2004 05:20 PM


All times are GMT -5. The time now is 10:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration