Shortening Numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shortening Numbers
# 1  
Old 02-07-2008
Shortening Numbers

Hi,

I'm using ksh and I'm trying to shorten a column of figures. For example, I'd like to turn:

3456789.9876 to 89.98
567.4956 to 67.49
4669493932.34564 to 32.34

... so the input figure will vary in length, I'm not worried about rounding, I just want the two figures either side of the dp. I probably know how to do it in awk and cut etc I just wondered if there was a more elegant way using say a number format in something like printf.

So far I haven't found a number format that will exclude figures starting from the extreme left (the millions, 100,000s etc). Anyone?

Last edited by Jonny2Vests; 02-07-2008 at 02:49 PM..
# 2  
Old 02-07-2008
use expr with % operator. n%100 will give you remainder so u'll get two digits.
u can use substr to extract two digits after decimal.

it's easier to use awk/cut. anyway, ur choice Smilie
# 3  
Old 02-07-2008
Thanks, but never used expr before, had a go, but dont really have the syntax, could you give me the whole line?

Did it with awk and cut but it was messy. Cant find a way to awk or cut the two decimal places left of the dp without doing a wc on the string, it only seems to work left to right.
# 4  
Old 02-07-2008
sed with some possibilities:

Code:
sed 's/.*\(..\...\).*/\1/'	#  format ##.##

sed 's/.*\(...\...\).*/\1/'	#  format ###.##

sed 's/.*\(....\...\).*/\1/'	#  format ####.##

sed 's/.*\(..\....\).*/\1/'	#  format ##.###

Regards
# 5  
Old 02-07-2008
Code:
echo '3456789.9876' | nawk '{match($0, /..[.]../); print substr($0, RSTART, RLENGTH)}'

# 6  
Old 02-07-2008
Quote:
Originally Posted by Jonny2Vests
Thanks, but never used expr before, had a go, but dont really have the syntax, could you give me the whole line?

Did it with awk and cut but it was messy. Cant find a way to awk or cut the two decimal places left of the dp without doing a wc on the string, it only seems to work left to right.
Code:
cat <<EOF | awk '{p=match($0,"\."); print substr($0,p-2,2)"."substr($0,p+1,2)}'
3456789.9876
567.4956
4669493932.34564
EOF

89.98
67.49
32.34
# 7  
Old 02-08-2008
Wow, thanks everyone, plenty to play around with.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

3. Shell Programming and Scripting

Shortening a awk command

The purpose of the below is to grab the 9th field of the data in "$epochtimeframe". and then translate that data (which is an epoch time), into a human readable form. echo $epochtimeframe | awk -F"--" '{print $9}' | awk -F"," '{print $2}' | perl -pe 's/(\d+)/localtime($1)/e' i'm sure many... (10 Replies)
Discussion started by: SkySmart
10 Replies

4. Shell Programming and Scripting

file name shortening for multiple directories

there was a post previously about this from around 2010 but i was unable to get the suggested scripts there to work. the following code works for me when it's saved inside the directory of files whose names i want to shorten, but i would like to be able to store it in a file with a list of ... (4 Replies)
Discussion started by: giseismology
4 Replies

5. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

6. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

7. Shell Programming and Scripting

Directory path shortening ????

As it was a new topic i started a new thread. There are different Directories under "$HOME/rsh/Desktop/at/bin/bellhop/ and $HOME/rsh/Desktop/at/examples/" i want to perform some operation like cp or mv Is it possible to do R=$HOME/rsh/Desktop/at/bin/bellhop/... (1 Reply)
Discussion started by: shashi792
1 Replies

8. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies
Login or Register to Ask a Question