How to control the decimal poins for the whole .txt file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to control the decimal poins for the whole .txt file?
# 1  
Old 10-10-2014
How to control the decimal poins for the whole .txt file?

Dear all,
I have a .txt file with n=400 columns, n=440,000 rows. The file looks like below:
-0.0625123454 0.03732424 -0.11293423 ...
0.15651325435 -0.1366346346 -0.28384636 ...
...

I want to change all the numbers in the file with n=3 decimal places. I don't know if it is very simple problem but I don't know how to do it. Thanks a lot for your help.

thanks,
Lin
# 2  
Old 10-10-2014
Quote:
Originally Posted by forevertl
Dear all,
I have a .txt file with n=400 columns, n=440,000 rows. The file looks like below:
-0.0625123454 0.03732424 -0.11293423 ...
0.15651325435 -0.1366346346 -0.28384636 ...
...

I want to change all the numbers in the file with n=3 decimal places. I don't know if it is very simple problem but I don't know how to do it. Thanks a lot for your help.

thanks,
Lin
Hello forevertl,

Could you please try following.

Code:
awk '{for(i=1;i<=NF;i++){if(i==NF){printf "%.3f\n",$i} else {printf "%.3f ",$i}}}'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-10-2014 at 05:01 PM..
# 3  
Old 10-10-2014
one issue

Hi,
Thanks a lot for the reply. I forgot to mention that there is some "NA" in the file, now using the code, it works well except for those NAs. The NA changed to 0.000 now. Is there a way that keep NA as NA? Thanks a lot!
Lin

Quote:
Originally Posted by RavinderSingh13
Hello forevertl,

Could you please try following.

Code:
awk '{for(i=1;i<=NF;i++){if(i==NF){printf "%.3f\n",$i} else {printf "%.3f ",$i}}}'  Input_file

Thanks,
R. Singh
# 4  
Old 10-10-2014
Quote:
Originally Posted by forevertl
Hi,
Thanks a lot for the reply. I forgot to mention that there is some "NA" in the file, now using the code, it works well except for those NAs. The NA changed to 0.000 now. Is there a way that keep NA as NA? Thanks a lot!
Lin
Hi forevertl,

Could you please try following and let me know.

Code:
awk '{for(i=1;i<=NF;i++){{if($i == "NA"){print $i} if(i==NF && $i != "NA"){printf "%.3f\n",$i} {if(i!=NF && $i != "NA"){printf "%.3f ",$i}}}}}' ORS=" "  Input_file

Thanks,
R. Singh
# 5  
Old 10-10-2014
seem it doesn't work. The total # of row in the output file changed. Thanks a lot.
Lin

Quote:
Originally Posted by RavinderSingh13
Hi forevertl,

Could you please try following and let me know.

Code:
awk '{for(i=1;i<=NF;i++){{if($i == "NA"){print $i} if(i==NF && $i != "NA"){printf "%.3f\n",$i} {if(i!=NF && $i != "NA"){printf "%.3f ",$i}}}}}' ORS=" "  Input_file

Thanks,
R. Singh
# 6  
Old 10-10-2014
Wrench

Hello,
It is working fine for me, as follows, let me know the error or output if you get any error, I have taken an example for same as follows.

Code:
Input file is as as follows:
cat file232
-0.0625123454 0.03732424 -0.11293423 NA

Code is as follows:
awk '{for(i=1;i<=NF;i++){{if($i == "NA"){print $i} if(i==NF && $i != "NA"){printf "%.3f\n",$i} {if(i!=NF && $i != "NA"){printf "%.3f ",$i}}}}}' ORS=" "  file232

Output is as follows:
-0.063 0.037 -0.113 NA

Thanks,
R. Singh
# 7  
Old 10-11-2014
It can be simplified like this

Input
Code:
akshay@nio:~$ cat file
-0.0625123454 0.03732424 -0.11293423 NA

Output
Code:
akshay@nio:~$ awk '{for(i=1;i<=NF;i++)if($i ~ /^-?[0-9]+/)$i+=0}1' CONVFMT="%.3f" file
-0.063 0.037 -0.113 NA

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

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

3. UNIX for Dummies Questions & Answers

How to control the decimal points for p-values in scientific format?

Dear all, I have a txt file with only one column which contains p values. My data looks like this: 5.04726976606584e-190 2.94065711152402e-189 2.94065711152402e-189 9.19932135717279e-176 1.09472516659859e-170 1.24974648916809e-170 0.1223974648916 0.9874974648916 ... what I want... (2 Replies)
Discussion started by: forevertl
2 Replies

4. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

5. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

6. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

7. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies

8. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies

9. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

10. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies
Login or Register to Ask a Question