LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-14-2013, 12:40 PM   #1
Bunty2013
LQ Newbie
 
Registered: Oct 2013
Posts: 9

Rep: Reputation: Disabled
Round up to 2 decimal place with the character at last


How to truncate upto 2 decimal place & keep the character at last in linux?

I am using the below command.

echo 123.45567M | awk '{printf("%.2f\n", $1)}'

Output
123.46

What I want as the output to be like this

123.46M

I want the "M" to be printed at last.
Any suggestions???
 
Old 10-14-2013, 01:08 PM   #2
Bunty2013
LQ Newbie
 
Registered: Oct 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Any suggestion???
 
Old 10-14-2013, 01:24 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Patience is a virtue? I saw your update just as I was about to post a suggestion, and nearly didn't...

Anyway, I would tend to use perl, rather than awk for something like this. In which case this works:

echo 123.45567M | perl -ne '($n,$u) = $_ =~ /([0-9.]*)([A-Z]*)/; printf "%0.2f%s\n",$n,$u'
 
1 members found this post helpful.
Old 10-14-2013, 01:32 PM   #4
Bunty2013
LQ Newbie
 
Registered: Oct 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks Robhogg for the help.One last question, can i use this script to fetch the data from TXT file and give the output.If yes, where should i be mentioning the file name.
 
Old 10-14-2013, 01:34 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Bunty2013 View Post
What I want as the output to be like this 123.46M
I want the "M" to be printed at last.
Try this:
Code:
echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}'
Daniel B. Martin
 
Old 10-14-2013, 01:36 PM   #6
Bunty2013
LQ Newbie
 
Registered: Oct 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks daniel. I have one more question. could you look into that.
 
Old 10-14-2013, 01:38 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Bunty2013 View Post
Thanks daniel. I have one more question. could you look into that.
State the question. If it is distinct from post #1 start a new thread.

Daniel B. Martin
 
Old 10-14-2013, 01:47 PM   #8
Bunty2013
LQ Newbie
 
Registered: Oct 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
can i use this script to fetch the data from TXT file and give the output.If yes, where should i be mentioning the file name.

suppose file name abc.txt

Data in the file contained

123.45567M
22.7867M
145.7689M

Can i use the script (echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}') suggestion by you and generate the output

123.46M
22.79M
145.77M
 
Old 10-14-2013, 01:49 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Bunty2013 View Post
Thanks daniel. I have one more question. could you look into that.
Maybe this is what you seek...
Code:
   Path=$(cut -d'.' -f1 <<< ${0})
 InFile=$Path"inp.txt"
OutFile=$Path"out.txt"
awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile >$OutFile
This code snippet assumes you have all three files (InFile, OutFile, and the script itself) in the same folder.

Daniel B. Martin
 
Old 10-14-2013, 01:53 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Bunty2013 View Post
can i use this script to fetch the data from TXT file and give the output.If yes, where should i be mentioning the file name.

suppose file name abc.txt

Data in the file contained

123.45567M
22.7867M
145.7689M

Can i use the script (echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}') suggestion by you and generate the output

123.46M
22.79M
145.77M
With this InFile ...
Code:
123.45567M
22.7867M
145.7689M
... this code ...
Code:
awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile >$OutFile
... produced this OutFile ...
Code:
123.46M
22.79M
145.77M
Daniel B. Martin
 
1 members found this post helpful.
Old 10-14-2013, 01:56 PM   #11
Bunty2013
LQ Newbie
 
Registered: Oct 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks Daniel.It was helpful to me.
 
Old 10-16-2013, 11:04 PM   #12
Bunty2013
LQ Newbie
 
Registered: Oct 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
I am using this script as suggested above by Daniel.It is working fine , but in some cases it is not.

The script
echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}'

Output
123.46M

If i am mentioning the below numbers in the script I am getting this output.

echo 123M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}'

Output
123.00M

Can anyone please suggest me if there is a number 123M in the script then it should print 123M only not 123.00M. And if there is a number with decimal like 123.45678M then it should print 123.46M. I want the both things in one script.
 
Old 10-17-2013, 06:49 AM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Bunty2013 View Post
... if there is a number 123M in the script then it should print 123M only not 123.00M. And if there is a number with decimal like 123.45678M then it should print 123.46M.
With this InFile ...
Code:
123.45567M
22.7867M
145.7689M
123M
... this code ...
Code:
awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile |sed 's/.00M/M/' >$OutFile
... produced this OutFile ...
Code:
123.46M
22.79M
145.77M
123M
Daniel B. Martin
 
Old 10-17-2013, 09:21 AM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Bunty2013 View Post
... if there is a number 123M in the script then it should print 123M only not 123.00M. And if there is a number with decimal like 123.45678M then it should print 123.46M.
With this InFile ...
Code:
123.45567M
22.7867M
145.7689M
123M
... this awk ...
Code:
awk -F "" '{if (index($0,".")>0) printf("%.2f%s\n",$0,$NF)
            else print $0'} $InFile >$OutFile
... produced this OutFile ...
Code:
123.46M
22.79M
145.77M
123M
Daniel B. Martin
 
Old 10-17-2013, 10:22 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I thought your original had more promise Daniel
Code:
awk -F "" '{f="%"(/\./?".2":".0")"f%s\n";printf f,$0,$NF}' file
 
1 members found this post helpful.
  


Reply



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
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 08:41 PM
[SOLVED] BC provides incorrect decimal results, but only on the last two decimal places... standard_output Linux - Newbie 4 06-27-2012 05:30 PM
[SOLVED] Mplayer place in KDE, lost icon, wrong place. firekage Slackware 4 01-24-2012 02:23 PM
PS2 mouse goes crazy [it goes round n round n round...] goci Linux - Hardware 2 10-09-2003 08:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:32 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
Open Source Consulting | Domain Registration