How to trim the decimal place for all the columns?

Dear all,
I have a file call test.txt which has 2000 columns, 1000 rows. If I want to trim all the columns to 3 decimal places, how can I do it? I know how to use awk prinf to trim specic columns. But I don't know how to trim all the columns. Thank you.

 
data sample: 
0.976004565 9.34567845 9.2345678 ...
2.345676879 4.68798909 2.5789090...
...

You'll indeed have to touch ALL columns individually. Show us your efforts so far.

I tried this code below and I got all the numbers trimed but all the columns merged to one single column. Anyone any any idea how I can change the code?

 
awk -F" " '{ for (i=1; i<=NF; i++) printf "%.3f\n", $0} }' test.txt > test2.txt

Hello forevertl,

You should make a very minor change as follows, though I don't know your complete requirement but as per your code I am trying to fix it, let me know how it goes.

 awk -F" " '{ for (i=1; i<=NF; i++) printf "%.3f\n", $i} }' test.txt > test2.txt
 

Thanks,
R. Singh

Thank you for your reply. I made the change and the code below doesn't work. All the columns still merged into one column.

awk -F" " '{ for (i=1; i<=NF; i++) printf "%.3f\n", $i }' test.txt > test2.txt

---------- Post updated at 10:46 AM ---------- Previous update was at 10:30 AM ----------

my original data example is as below. (the actually data has more columns and rows)

 
9.1602631246 8.5063248058 8.3995683976 8.3076705408 8.1020699639
7.495059918 7.4248422511 6.7701030427 7.6117117914 7.891185464
7.6077110958 6.4018191304 9.7362209332 8.4924611613 8.543516433

The data I want to get is:

9.160 8.506 8.399 8.307 8.102
7.495 7.424 6.770 7.611 7.891
7.607 6.401 9.736 8.492 8.543

But if I use the code below, I got all the columns trimed but all merged into one column. Anyone kow how to fix it? Thank you.

awk -F" " '{ for (i=1; i<=NF; i++) printf "%.3f\n", $i }' test.txt > test2.txt

Try:

awk '{for(i=1; i<=NF; i++) $i+=0}1' CONVFMT="%.3f" file
3 Likes

This code works well. Thank you very much.
Lin