How to trim the decimal place for all the columns?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to trim the decimal place for all the columns?
# 1  
Old 04-19-2016
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.
Code:
 
data sample: 
0.976004565 9.34567845 9.2345678 ...
2.345676879 4.68798909 2.5789090...
...

# 2  
Old 04-19-2016
You'll indeed have to touch ALL columns individually. Show us your efforts so far.
# 3  
Old 04-19-2016
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?
Code:
 
awk -F" " '{ for (i=1; i<=NF; i++) printf "%.3f\n", $0} }' test.txt > test2.txt

# 4  
Old 04-19-2016
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.
Code:
 awk -F" " '{ for (i=1; i<=NF; i++) printf "%.3f\n", $i} }' test.txt > test2.txt

Thanks,
R. Singh

Last edited by Scrutinizer; 04-19-2016 at 12:42 PM.. Reason: Changed lime to a readable color
# 5  
Old 04-19-2016
Thank you for your reply. I made the change and the code below doesn't work. All the columns still merged into one column.
Code:
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)
Code:
 
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:
Code:
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.
Code:
awk -F" " '{ for (i=1; i<=NF; i++) printf "%.3f\n", $i }' test.txt > test2.txt


Last edited by Scrutinizer; 04-19-2016 at 12:42 PM.. Reason: code tags; Changed lime to a readable color
# 6  
Old 04-19-2016
Try:
Code:
awk '{for(i=1; i<=NF; i++) $i+=0}1' CONVFMT="%.3f" file

These 3 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-19-2016
This code works well. Thank you very much.
Lin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zero padding a Number before and after a decimal place

Hi I was hoping someone could help me with a sed script I am trying to write? I am on a Mac running ElCapitan I have some text that I have converted from a pdf that I want to format into an xml file. In the file I have managed to delete all the text I do not need. The text I have left is... (8 Replies)
Discussion started by: Paul Walker
8 Replies

2. Shell Programming and Scripting

Using awk to place decimal points at proper position

Hi, I have one input file which is delimited by pipe. I want to put decimal points in this input file at particular position in particular column and also get the negative sign (if any) at start of that column. $ cat Input_file.txt 11|10102693|1|20151202|10263204|20151127|N|0001... (7 Replies)
Discussion started by: Prathmesh
7 Replies

3. UNIX for Dummies Questions & Answers

How to set decimal place in awk?

Dear all, I have a data test.txt as below. X22.30799720_T cg03868770 -0.5645412582127 2.4084685750406e-175 X22.30781182_A cg03868770 -0.5620426397492 3.5818034129169e-172 X22.30780724_C cg03868770 -0.5616890165605 2.9765569717858e-168 what I want is: X22.30799720_T cg03868770... (3 Replies)
Discussion started by: forevertl
3 Replies

4. Shell Programming and Scripting

Unable to get the Specific column with 2 decimal place

Hi, I have an issue converting decimal places of a particular column, i am using below script to get the output, but the output is not generating in desired format. awk -F"," 'BEGIN{OFS=","}{if(NR==0)getline;if ($7 != "") {if ($7 > 0) $7=$7/100 ; {printf "%.2f"... (3 Replies)
Discussion started by: rramkrishnas
3 Replies

5. Shell Programming and Scripting

awk if condition match and fix print decimal place

Hi All, I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6. See the sample : before: Sort Total Site Sort SortName Parts ... (3 Replies)
Discussion started by: horsepower
3 Replies

6. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

7. Shell Programming and Scripting

How to trim the zero's after decimal?

Hello all, I have an XML with below content from which i need to remove the trailing zeros, like 123.00 should be converted to 123 and 123.01200 to 123.012 Below is the sample excerpt data from XML file. My input file size could be approximately 5 GB or less. CURRENT:... (10 Replies)
Discussion started by: Ariean
10 Replies

8. Shell Programming and Scripting

Changing integer columns to floating decimal columns

I have a document that has 7 columns. eg. $1 $2 $3 $4 $5 $6 $7 string string string string integer integer integer The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all... (3 Replies)
Discussion started by: kadm
3 Replies

9. Shell Programming and Scripting

Script to place selected columns from a group of files side by side in a new file

Hi Everyone, I need a shell/perl script to bring selected columns from all the files located in a directory and place them in a new file side by side. File1: a b c d 2 3 4 5 f g h i .......... File2: I II III IV w x y z .............. and so on many files are there...... (8 Replies)
Discussion started by: ks_reddy
8 Replies

10. Shell Programming and Scripting

add numbers with decimal place in UNIX

i am trying to add numbers with decimal place and I am prompted with an error. this expr command works :add=`expr 1 + 1` :echo $add :2 but when i am trying to add :addThis=`expr 1.1 + 1` :expr: An integer value was expected. is there a way to add numbers with decimal place in UNIX? (4 Replies)
Discussion started by: tads98
4 Replies
Login or Register to Ask a Question