Unable to get the Specific column with 2 decimal place


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to get the Specific column with 2 decimal place
# 1  
Old 09-10-2015
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.


Code:
awk -F"," 'BEGIN{OFS=","}{if(NR==0)getline;if ($7 != "") {if ($7 > 0) $7=$7/100 ; {printf "%.2f" $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20 > "2.txt"} } }' "1.txt" ;

I am deviding 7th Column field by 100, and want the output of 7th column with 2 decimal along with other columns.

For example if one value post division is
Code:
123.12345

then output should be as
Code:
123.12

wihtout rounding up of decimal value.
# 2  
Old 09-10-2015
NR will always be > 0, so that if condition is pointless. Your FMT string is for one single number only, so the output will be very limited. Except for the header (which has an unnecessary comma in the end), your file has only records with 20 fields, so the enumeration of fields needn't be. ($7 > 0) implies ($7 != ""), so the latter can be left out. Try
Code:
awk -F"," '
BEGIN   {OFS=","
         OFMT="%.2f"
        }
        {if ($7 > 0) $7=$7/100
        }
1
' /tmp/1.txt

Subtract 0.005 from the $7 result if you want to disable rounding up
# 3  
Old 09-10-2015
Thanks RudiC for your support, I am trying with the code you provided with litle modification but the code is not providing any output,
Code:
awk -F"," '
BEGIN   {OFS=","
         OFMT="%.2f"
        }
        {
        {if ($7 > 0) $7=$7/100}
print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20  > 2.txt}
'1.txt

, Please help me to identifying where I am going wrong.
Thanks
# 4  
Old 09-10-2015
1) Modify working solutions STEP BY STEP only! That will enable you to identify and concentrate on the step that goes wrong.

You need to double quote the "2.txt" as you did in your first post.

And, the if ($7 ... line could be written as
Code:
$7 > 0     {$7=$7/100}

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

Help with Round Up with 2 decimal point at specific column

Input file: USA 20.5683 UK 3.54221 Japan 2.54001 China 2.50897 Germany 2.05816 . . Desired output file: USA 20.57 UK 3.54 Japan 2.54 China 2.51 Germany 2.06 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

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

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

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

6. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

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

8. Shell Programming and Scripting

unable to return a decimal value from a function

Hi Guys, I am unable to return a decimal value from a function to the main script. I am getting the correct value in the function but when it is returning to the main script using "return" it is coming as 0. Below is my code. read VALUE_V fun_FATHERID fun_SONID fun_TOPID fun_RTYPE <... (2 Replies)
Discussion started by: mac4rfree
2 Replies

9. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 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