number of digits after decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting number of digits after decimal
# 1  
Old 10-21-2011
number of digits after decimal

Hi All,

I have a file of decimal numbers,


Code:
 
cat file1.txt

1.1382666907
1.2603107334
1.6118799297
24.4995857056
494.7632588468
560.7633734425
.....

I want to see the output as only 7 digits after decimal


Code:
 
cat output.txt

1.1382667
1.2603107
1.6118799
24.4995857
494.7632589
560.7633734
...

Thanks in advance,

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by senayasma; 10-21-2011 at 05:56 PM..
# 2  
Old 10-21-2011
Code:
 nawk '{printf("%.7f\n", $1)}'  myFile
OR
nawk '{print $1+0}' OFMT='%.7f'  myFile


Last edited by vgersh99; 10-21-2011 at 06:15 PM..
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-21-2011
Not pretty, but here it is with cut

Code:
for line in $(cat file1.txt); do whole=$(echo $line | cut -d. -f1); fraction=$(echo $line | cut -d. -f2 | cut -c 1-7); echo "$whole.$fraction"; done

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-21-2011 at 06:29 PM.. Reason: code tags, please!
# 4  
Old 10-21-2011
# 5  
Old 10-22-2011
one more ..
Code:
$ while read i;do echo "scale=7;$i/1" | bc;done<infile

# 6  
Old 10-24-2011
Move the pipe out of the loop, so you run bc once to process all the data instead of running bc 376 separate times to process 376 lines.

Code:
while read i;do echo "scale=7;$i/1" ; done<infile | bc


Last edited by Corona688; 10-24-2011 at 01:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing with decimal places from last 4 digits

I have input file like below, 201424|9999|OSS|622010|RGT|00378228764 201424|8888|OM|587079|RGT|00284329675 201424|7777|OM|587076|RGT|00128671024 201424|6666|OM|581528|RGT|00113552084 Output should be like below, should add decimal (.) from last 4 digits. ... (2 Replies)
Discussion started by: vinothsekark
2 Replies

2. Shell Programming and Scripting

Find number of digits in a word

HI, Can you tell me how to find the number of digits in a word. $cat data.txt +123456ad 87645768 Output should be 6 8 (5 Replies)
Discussion started by: ashwin3086
5 Replies

3. Shell Programming and Scripting

awk changes to cut number of digits

HCPM1ONDB00014800011800000589009211201 L201307022013070228AUD 00000000031. 000965105800000000000000000000000 MOBITV KEYA ... (4 Replies)
Discussion started by: mirwasim
4 Replies

4. Shell Programming and Scripting

extracting Number variable and the following digits.

HI all, I have output of something like this: crab: ExitCodes Summary >>>>>>>>> 12 Jobs with Wrapper Exit Code : 50117 List of jobs: 1-12 See https:///twiki/something/ for Exit Code meaning crab: ExitCodes Summary >>>>>>>>> 5 Jobs with Wrapper Exit Code : 8001 List of... (20 Replies)
Discussion started by: emily
20 Replies

5. Shell Programming and Scripting

summing the digits of a binary nuMBER

please help me write a perl program to find the difference of 1 and zeros of a 6 digit binary number. eg If input is 111100 expected output +2 if input is 000011 expected output -2 input is 000111 expected output 0 (2 Replies)
Discussion started by: dll_fpga
2 Replies

6. 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

7. Shell Programming and Scripting

how to sort date in decimal values uptp two digits

Hi all, there is a data in a file wich loks likes 00:00:49|24.48| 00:01:49|22.83| 00:02:49|22.07| 00:03:49|20.72| 00:04:49|21.28| 00:05:49|21.22| 00:06:49|21.38| 00:07:49|20.93| 00:08:49|21.27| 00:09:49|20.65| 00:10:49|19.42| 00:11:49|21.93| 00:12:49|20.62| 00:13:49|20.23|... (3 Replies)
Discussion started by: jojo123
3 Replies

8. Shell Programming and Scripting

Count number of digits in a word

Hi all Can anybody suggest me, how to get the count of digits in a word I tried WORD=abcd1234 echo $WORD | grep -oE ] | wc -l 4 It works in bash command line, but not in scripts :mad: (12 Replies)
Discussion started by: ./hari.sh
12 Replies

9. UNIX for Advanced & Expert Users

restrain the number of digits of a PID

How is it possible under UNIX to restrain the number of digits of the PID number? For instance, we have a product that generates a PID of 7 digits, and we would like to have only 6 digits maximum instead for the PID. Thank you for your help. (1 Reply)
Discussion started by: mlefebvr
1 Replies
Login or Register to Ask a Question