Reducing decimal points
1
0
Entering edit mode
2.9 years ago
Ap1438 ▴ 50

I have a file with columns in numbers as data which are in multiple decimal points. i want to change those multiple decimal point data into 2 decimal points.

E.g.

k__Bacteria|p__Proteobacteria 2|1224        18.7582950961843     20.1583624755941     
k__Bacteria|p__Actinobacteria 2|201174      13.113604074899      13.9444369113068        

to

k__Bacteria|p__Proteobacteria 2|1224       18.75    20.15       
k__Bacteria|p__Actinobacteria 2|201174      13.11      13.94      
sed • 952 views
ADD COMMENT
5
Entering edit mode
2.9 years ago
Mensur Dlakic ★ 28k

Assuming your data is saved in file.txt:

awk '{printf "%s %s %.2f %.2f\n", $1, $2, $3, $4}' file.txt

This assumes that strings are present in columns 1-2 and prints them unchanged, and reduces numbers in columns 3-4 to two decimals.

If you want the resulting columns to be tab-separated:

awk '{printf "%s\t%s\t%.2f\t%.2f\n", $1, $2, $3, $4}' file.txt
ADD COMMENT
0
Entering edit mode

It worked thank you for your valuable time and suggestion.

ADD REPLY

Login before adding your answer.

Traffic: 1847 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6