Print a number up to last significant digit after decimal point


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print a number up to last significant digit after decimal point
# 8  
Old 08-04-2012
Code:
 echo "$x" | awk '{sub("0*$","",$0);sub("\\.$",".0",$0)}1'

# 9  
Old 08-04-2012
Thanks burtas11. That serves the purpose.
# 10  
Old 08-04-2012
burtas11 has shown you how to do it with awk.

For sed try:
Code:
echo "$x" | sed '/[1-9]0*$/s/0*$//
/[.]0*$/s/0*$/0/'

And, at least for ksh, this should work in a shell script:
Code:
y="${x%%*([0])}"
if [ "$y" == "${y%.}." ]
then	y="${y}0"
fi
echo $y

# 11  
Old 08-04-2012
In bash, according to what you like better in case of round integers (most important parts in bold):

Code:
r=0.9000
s=10.0000
t=91.0900
shopt -s extglob

for i in $r $s $t; do echo ${i%%?(.)*(0)}; done
0.9
10
91.09

for i in $r $s $t; do a=${i%%*(0)}; echo ${a/%./.0}; done
0.9
10.0
91.09

--
Bye
# 12  
Old 08-05-2012
Quote:
Originally Posted by bartus11
Code:
 echo "$x" | awk '{sub("0*$","",$0);sub("\\.$",".0",$0)}1'

This works fine. But when I use
Code:
x=`echo "$x" | awk '{sub("0*$","",$0);sub("\\.$",".0",$0)}1'`

I get the following error message.

awk: warning: escape sequence `\.' treated as plain `.'

and also I don't get the expected value when I do echo $x

Last edited by Franklin52; 08-06-2012 at 03:50 AM.. Reason: Please use code tags for data and code samples
# 13  
Old 08-05-2012
When you're using command substitution with commands that contain single quotes, you need to know that the quoting rules change. $(...) is almost always easier to get right without having to think about it too much as opposed to `...`.
The command:
Code:
x=$(echo "$x" | awk '{sub("0*$","",$0);sub("\\.$",".0",$0)}1')

will do what you wanted.
# 14  
Old 08-05-2012
Quote:
Originally Posted by Don Cragun
When you're using command substitution with commands that contain single quotes, you need to know that the quoting rules change. $(...) is almost always easier to get right without having to think about it too much as opposed to `...`.
The command:
Code:
x=$(echo "$x" | awk '{sub("0*$","",$0);sub("\\.$",".0",$0)}1')

will do what you wanted.
Yes, it works now. Thanks. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

3. UNIX for Beginners Questions & Answers

How to add following decimal point to a CSV value?

hi there I being trying to figure out way to add " .0" to an integer value in a csv using sed or awk with out success. just as a work around for 2147483647 32 bit limitation that influxdb is currently having the data base will accept values and work fine if it has the XXX.0 ... (7 Replies)
Discussion started by: sash99
7 Replies

4. Shell Programming and Scripting

remove directories with two digits after decimal point

Hi everyone, I am new here and generally not experienced with linux. My question must be easy, but as for now I have no idea how to do it. I have lots of directories with numerical names, e.g. 50 50.1 50.12 etc. What I want is to leave directories with no or single digit after the decimal... (2 Replies)
Discussion started by: cabaciucia
2 Replies

5. Shell Programming and Scripting

awk length of digit and print at most right digit

Have columns with digits and strings like: input.txt 3840 3841 3842 Dav Thun Tax Cahn 146; Dav. 3855 3853 3861 3862 Dav Thun Tax 2780 Karl VI., 3873 3872 3872 Dav Thun Tax 3894 3893 3897 3899 Dav Thun Tax 403; Thun 282. 3958 3959 3960 Dav Thun Tax 3972 3972 3972 3975 Dav Thun Tax... (8 Replies)
Discussion started by: sdf
8 Replies

6. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

7. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

8. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

9. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies
Login or Register to Ask a Question