change precision of bash variable

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change precision of bash variable
# 1  
Old 06-10-2011
change precision of bash variable

I have a variable in a bash script,
DISTANCE=`awk 'BEGIN {FS="\t"} {if (NR==2) print $3;}' $OUTFILE`

this is a real number taken from a file. The value are like,

I am using this value in a file name,

I would like to shorten the number some to something like 0.33. I am assuming that these are strings and not numbers. There is no real need for anything like rounding if that is difficult. If I could just print the first 4 characters, or something like that, that would be a big improvement.


Last edited by radoulov; 06-11-2011 at 04:51 AM.. Reason: Code tags.
# 2  
Old 06-11-2011
% printf '%.2f\n' 0.334561754018 

This User Gave Thanks to yazu For This Post:
# 3  
Old 06-11-2011
Thanks, I got it to work with,
DISTANCE=`printf '%.2f' $DISTANCE`

# 4  
Old 06-11-2011
Or you can just modify your awk command in order to produce the desired output:
  awk 'BEGIN { FS = "\t" } 
  NR == 2 { 
      printf "%.2f\n", $3
      }' "$OUTFILE"

If for any reason that's not possible, with bash >= 3 alternatively you could use something like this:

printf -vDISTANCE '%.2f' $DISTANCE

# 5  
Old 06-11-2011
that with the code provided, depending on the distance value, you may get a result of more than 4 character.

[ctsgnb@shell ~/sand]$ printf '%.2f\n' 1234.334561754018
[ctsgnb@shell ~/sand]$ printf '%.4s\n' 1234.334561754018
[ctsgnb@shell ~/sand]$ printf '%.4s\n' 0.334561754018
[ctsgnb@shell ~/sand]$ printf '%.4s\n' .33456175
[ctsgnb@shell ~/sand]$ printf '%.2f\n' .33456175

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Precision in print

I have a shell script which prints a variable to a text file (sorry if this is the wrong terminology, I'm new to this): awk `{sumdiffsqt+=$1;stdevt=sqrt(sumdiffsqt/('${n}'-1));errt=stdevt/sqrt('${n}')} END {print '$period',stdevt,errt}' diffsqt.txt >> ${xstation1}_${xstation2}_tt.err I need... (4 Replies)
Discussion started by: claire.a
4 Replies

2. Shell Programming and Scripting

How to change Linux Terminal environment variable in a perl or bash script?

Hi, I meet an problem that it cannot change Terminal environment variable in a perl or bash script. This change can only exist and become effective in script lifetime. But I want to make this change take effect in current opened Terminal. In our view, the thought seems to be impossible, As... (9 Replies)
Discussion started by: weichanghe2000
9 Replies

3. Shell Programming and Scripting

Get timestamp with millisecond precision

Hi All, could any body let me know. how to get timestamp with millisecond precision in unix bash shell. example -->2005-12-06- 4-22-35-195 please help me. Thanks, Krupa:wall: (3 Replies)
Discussion started by: krupasindhu18
3 Replies

4. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to I... (2 Replies)
Discussion started by: div_Neev
2 Replies

5. Shell Programming and Scripting

How can i change my bash prompt ?

It looks like, user@hostname:/auto/home3/user$ Desired, user@hostname$ I added following line in .bashrc, but still its same. export PS1=" $ " Please help me :confused: (13 Replies)
Discussion started by: admax
13 Replies

6. UNIX for Dummies Questions & Answers

Bash encoding, how to change

Hey guys. The problem is : i need to change encoding (to be more precise UTF-8) or change the language . You see , when i log in , manuals are shown in 'Some symbols' (being written in 'Not English') and its very confusing to work. Please Help :) (3 Replies)
Discussion started by: IdleProc
3 Replies

7. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

8. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

9. Programming

max precision in float

Hi, As all of us know that size of int is machine as well as compiler dependent we can get the range and size of an int , float and char etc in /usr/include/limits.h header file of a compiler but could any one tell me how to get info of precision of float and ... (6 Replies)
Discussion started by: useless79
6 Replies

10. UNIX for Dummies Questions & Answers

precision getting?

I need to find out the precision on a solaris machine using the unix terminal. How do I find this out? Specifics needed: How are single precision numbers represented (8 or 16 bit) How are double precision numbers represented (16 or 32 bit) How are they stored. ( some computers store numbers... (1 Reply)
Discussion started by: Townsfolk
1 Replies
Login or Register to Ask a Question