How to set decimal place in awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to set decimal place in awk?
# 1  
Old 12-14-2015
How to set decimal place in awk?

Dear all,
I have a data test.txt as below.
Code:
 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:
Code:
 X22.30799720_T cg03868770 -0.565 2.408e-175
X22.30781182_A cg03868770 -0.562 3.582e-172
X22.30780724_C cg03868770 -0.562 2.977e-168

anyone know how to do that? Thank you.
Lin

Last edited by jim mcnamara; 12-14-2015 at 01:06 PM.. Reason: fixed code tags use
# 2  
Old 12-14-2015
Try:
Code:
awk '{printf "%s %s %.3f %.3e\n", $1, $2, $3, $4}' file

or
Code:
awk '{$3=sprintf("%.3f",$3); $4=sprintf("%.3e",$4)}1' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-14-2015
awk uses format specifiers very similar to C.
Code:
printf "%.3f  %.3e\n", 1234.12345 1234.12345
1234.123  1.234e+03

So,
Code:
awk '{printf("%s %s %.3f  %.3e\n", $1, $2, $3, $4)}'  inputfile > outputfile

oops - Scrutinizer beat me to it Smilie
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 12-14-2015
I tried the first code and it works. Thanks a lot!
 
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. UNIX for Dummies Questions & Answers

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... (6 Replies)
Discussion started by: forevertl
6 Replies

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

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. Shell Programming and Scripting

awk pattern matching in place of sed

I have written a script to parse data from some files on a Solaris 10 system and send the output to a CSV formatted file. The code snipped i am using to pull the data is as follows.... src_line=$(sed -n "/^search_pattern$/{=;}" $file) for i in $src_line do start_line1=$(( i + 9 )) nawk -v... (4 Replies)
Discussion started by: electricheadx
4 Replies

7. Shell Programming and Scripting

awk: setting decimal

hello, when I type the following command awk -v varc="$i" '{ new=($1*varc)} { print new }'it gives outputs in 5 decimal. How can I set my outputs to 9 decimal by using awk. thanks (5 Replies)
Discussion started by: rpf
5 Replies

8. Shell Programming and Scripting

Four decimal places with awk

i have a script in which awk prints "($2-1700)/10000" and the answer is -0.07,but i want the answer in 4 decimal places. that is -0.0700. How can i sue awk to get my results in four decimal places (4 Replies)
Discussion started by: tomjones
4 Replies

9. Shell Programming and Scripting

Place number with awk

Hello, if I've a list of number 23 34 56 78 how I can place a sequence of ordinated number in a boundary column so 1 23 2 34 3 56 4 78 Thanks in advance! (3 Replies)
Discussion started by: cv313x
3 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