Plz help me out use OFMT operator in awk programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Plz help me out use OFMT operator in awk programming
# 1  
Old 02-19-2016
Plz help me out use OFMT operator in awk programming

Hi
While calculating the sum of 6 and 7 fileds it is calculate the wrong value because of floating point like 7898778.10

Code:
awk '
BEGIN { FS = OFS = "|"
}
NR == 1 {
        next
}
{       UX = $1 OFS $3 OFS $4 OFS $5
 
        p1[UX] +=$6
        p2[UX] +=$7
}
END {   for(i in UX)
                printf("%s|%d|%d\n",UX, p1[UX], p2[UX] )
}' file


Last edited by jagu; 02-19-2016 at 05:48 AM..
# 2  
Old 02-19-2016
I'd be very surprised if awk would "calculate the wrong value because of floating point". And, I don't see how OFMT could help you in a formatted print.

What I can say is that
- you try to loop over a simple variable (UX) instead an array (e.g. p1). This should yield an error.
- you printf with the %d format specifier, so you'll get integer output and can't expect floating point.

---------- Post updated at 11:06 ---------- Previous update was at 11:04 ----------

Plus, in the loop you don't use the loop variable i to index the arrays but use the constant UX.
# 3  
Old 02-19-2016
Hi Rudic
Plz help me out cancatenate the 1,2,3,4 ,5 and sum of 6 and 7 fileds should display in single line
6 and 7 th fileds are 8 digit floting point like 12345.23
Input file
Code:
1 2 3 4 5 12345.23 67809.45
1 2 3 4 5 12345.23 67809.45
1 2 3 4 5 12345.23 67809.45
1 2 3 4 5 12345.23 67809.45
1 2 3 4 5 12345.23 67809.45

output should be
Code:
1 2 3 4 5 61726.15 339047.25

Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for full-line and multi-line sample input, sample output, and for code segments.

Last edited by Don Cragun; 02-19-2016 at 12:53 PM.. Reason: Change ICODE tags to CODE tags.
# 4  
Old 02-19-2016
Try (untested)
Code:
END   {for(i in p1)   printf("%s|%f|%f\n", i, p1[i], p2[i] ) }

# 5  
Old 02-19-2016
using ur code the value display wrong like 101.000000|451.000000 it should be .2 decimal plzzzzzzz

Last edited by Don Cragun; 02-19-2016 at 12:54 PM.. Reason: Add ICODE tags.
# 6  
Old 02-19-2016
Try (untested)
Code:
END   {for(i in p1)   printf("%s|%.2f|%.2f\n", i, p1[i], p2[i] ) }

# 7  
Old 02-19-2016
I checked it display 2 dercimal number and but the numeber are in correct checked sum should be diplay like
Code:
2481298.32
248112298.32
24812458.32


Last edited by Don Cragun; 02-19-2016 at 12:55 PM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using like operator in awk if condition

Hello All, I have developed a script which selects a particular filed from a file ,trims it,searches for a particular pattern and then mail it when found. cat test_file.txt |sed -n '5,$p'|sed -e 's/ //g'|awk -F'|' '{if ($4 !="Alive") print $1,$2,$3,$4}' >> proc_not_alive.txt It is... (4 Replies)
Discussion started by: karthik adiga
4 Replies

2. UNIX for Dummies Questions & Answers

awk if statement / equals operator

Hi, I was hoping someone could explain this please :) I'm using bash, scientific linux... and I don't know what else you need to know. With awk '{ if( 0.3 == 0.1*3) print $1}' file.dat nothing will be printed since apparently the two numbers do not equate. (Using 0.3 != 0.1*3 is seen... (4 Replies)
Discussion started by: Golpette
4 Replies

3. Shell Programming and Scripting

awk Help - Comparison Operator problem

Hi, I've tried searching through the forum but I've drawn a blank so i'm going to post here. I'm developing a number of checks on a CSV file, trying to find if any are greater than a max limit. I'm testing it by running it from a command line. The file I'm testing has 8 records. When I... (3 Replies)
Discussion started by: Tmart
3 Replies

4. Shell Programming and Scripting

Awk with or and not operator

awk -F '/' '{ if (($2!="30") print }' f.dat > date_mismatch.dat The above command is giving the desired results But I would want use Or in the above but it is returning the complete file awk -F '/' '{ if ($2!="30"||$2!="31") print }' f.dat > date_mismatch.dat Have tried the... (1 Reply)
Discussion started by: infernalhell
1 Replies

5. Homework & Coursework Questions

Plz Help Me in This question in Shell Programming

2- Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is given, the script should count and report the number of files in this directory. Only regular files should be counted, not... (1 Reply)
Discussion started by: tahseen_22334
1 Replies

6. Shell Programming and Scripting

Plz Help Me in This question in Shell Programming

2- Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is given, the script should count and report the number of files in this directory. Only regular files should be counted, not... (1 Reply)
Discussion started by: tahseen_22334
1 Replies

7. Shell Programming and Scripting

plz, i wait your help, AWK problem

I have tracefile of three nodes (0 , 1 and 2 ) as follows: + 0.02 0 1 tcp 40 ------- 1 0.0 2.0 0 0 - 0.02 0 1 tcp 40 ------- 1 0.0 2.0 0 0 + 0.02 2 1 tcp 40 ------- 2 2.1 0.1 0 1 - 0.02 2 1 tcp 40 ------- 2 2.1 0.1 0 1 r 0.025032 0 1 tcp 40 ------- 1 0.0 2.0 0 0 + 0.025032 1 2 tcp 40 -------... (11 Replies)
Discussion started by: ASAADAOUI
11 Replies

8. Shell Programming and Scripting

Explain this AWK script plz

Hi frnds, one my frnds has given resolution for my problem as below. it working great , but i couldnt understand somethings in the script. Why ++ operator after the function calling. how these each block working. will each run for each input line sequencially or one block for all the lines... (9 Replies)
Discussion started by: Gopal_Engg
9 Replies

9. Shell Programming and Scripting

Very New To Unix Shell Programming:Plz Help

Hi Gurus I am very new to Unix Shell Prog. I have a file in format Q1 Dirname-FileName Score Remarks i.e. containing columns separated by space. I want to read Column 1 and 2 and then join them to make a string that would be a path to a file. I will use this string to fetch the files and... (4 Replies)
Discussion started by: kimskams80
4 Replies

10. UNIX for Dummies Questions & Answers

plz Help How should I configure cc compiler output file plz help???

i.e configuration of C compiler :confused: (4 Replies)
Discussion started by: atiato
4 Replies
Login or Register to Ask a Question