LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-23-2013, 04:24 AM   #1
cazanadrian
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Rep: Reputation: Disabled
division in awk


I have 3 columns in a file containing numbers
How can I display this columns divided by 100 without decimals separated by /

Code:
4387205283,4184230491,202974792

awk '{ print "" $1/100 "/" $2/100 "/" $3/100 "" }'
expected result
Code:
43872052/41842304/2029747
Thank you
 
Old 01-23-2013, 05:12 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
This gets the fields in the wrong order. I've added 50 for rounding rather than truncation when /100.
Code:
awk '{split($0,a,","); comma=""; for (n in a) {printf("%s%d", comma, (a[n]+50)/100); comma="/"}};END{print "\n"}' input_filename
 
Old 01-23-2013, 05:51 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Slightly shorter alternative:
Code:
awk -F, '{for(i=1;i<=NF;i++)printf "%i%c",$i/100,i==NF?"\n":"/"}'
 
Old 01-23-2013, 06:17 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Another one
Code:
awk 'BEGIN{FS = ","; OFS = "/"; OFMT = "%.0f"}{print $1/100, $2/100, $3/100}' file
Results are rounded (not truncated).
 
1 members found this post helpful.
  


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
[SOLVED] [awk] division by NaN dhodho Programming 10 10-15-2012 06:38 PM
how to handle division by zero in awk dazdaz Programming 2 05-29-2011 07:15 PM
Division by 0 error cat555 Linux - Newbie 8 01-22-2009 04:58 PM
file division using C fs11 Programming 46 08-17-2007 05:04 PM
C++ - Division by 0 and 'nan' Nightfox Programming 1 05-03-2006 11:58 AM


All times are GMT -5. The time now is 08:57 AM.

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