Sum value in text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum value in text file
# 1  
Old 08-30-2017
Sum value in text file

I need to sum values in text file in case duplicate row are present with same name and different value below is example of data in file i have and format i need.


Data in text file

Code:
20170308
PM,U,2
PM,U,1
PM,U, 113
PM,I,123
PM,U,123
DA,U 135
DA,I,113
DA,I,1
DA,U,22
20170309
PM,U,2
PM,U,1
PM,U, 23
PM,I,123
PM,I,1
PM,U,123
DA,U 135
DA,I,113
DA,I,1
DA,U,22


Output i need in file

Code:
20170308
PM,U, 115
PM,I,123
PM,U,123
DA,U 135
DA,I,114
DA,U,22
20170309
PM,U, 26
PM,I,123
PM,I,1
PM,U,123
DA,U 135
DA,I,114
DA,U,22


Last edited by vgersh99; 08-30-2017 at 02:48 PM.. Reason: code tags, please!
# 2  
Old 08-30-2017
where exactly are you stuck?
# 3  
Old 08-30-2017
Quote:
Originally Posted by vgersh99
where exactly are you stuck?
I need to sum of values (Third column) for rows having same name (column 1 and column 2) it should sum only within that date range.
Refer my example .I tried to do this with awk but no luck
# 4  
Old 08-30-2017
Quote:
Originally Posted by Adfire
I need to sum of values (Third column) for rows having same name (column 1 and column 2) it should sum only within that date range.

Refer my example .I tried to do this with awk but no luck


Please show your attempts and what issues you're facing.


Sent from my iPhone using Tapatalk
# 5  
Old 08-30-2017
Quote:
Originally Posted by vgersh99
Please show your attempts and what issues you're facing.


Sent from my iPhone using Tapatalk
I am pretty new to unix tried few solution from google .So looking for some help.
# 6  
Old 08-30-2017
Quote:
Originally Posted by Adfire
I am pretty new to unix tried few solution from google .So looking for some help.


Once again, where are with your own attempts as you've claimed?


Sent from my iPhone using Tapatalk
# 7  
Old 08-30-2017
Quote:
Originally Posted by vgersh99
Once again, where are with your own attempts as you've claimed?


Sent from my iPhone using Tapatalk
I don't have it right now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Sum duplicate values in text file through awk between dates

I need to sum values in text file in case duplicate row are present with same name and different value below is example of data in file i have and format i need. Data in text file 20170308 PM,U,2 PM,U,113 PM,I,123 DA,U,135 DA,I,113 DA,I,1 20170309 PM,U,2 PM,U,1 PM,I,123 PM,I,1... (3 Replies)
Discussion started by: Adfire
3 Replies

4. Shell Programming and Scripting

Sum numeric columns contained in a plain text file

Hi everyone, Here are the contents of a plain text file created by a SQL query: SUM(T.TRNQTY) COUNT(D.TRNSEQ) ---------------- ---------------- 1380 46 1393 59 2680 134 740 37 ... (5 Replies)
Discussion started by: gacanepa
5 Replies

5. Shell Programming and Scripting

Sum of two columns in a file

Hi, I need to do a sum of two columns in a file where delimiter is |^ input 1|^2|^3|^4|^50|^2|^3|^100 2|^3|^4|^6|^100|^7|^2|^50 3|^4|^2|^3|^50|^6|^3|^50 4|^2|^5|^7|^25|^2|^8|^25 Output required: Sum of 2 columns 5 & 8 which is 450 (11 Replies)
Discussion started by: Jram
11 Replies

6. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

7. UNIX for Dummies Questions & Answers

Sum up a decimal column in a tab separated text file and error handling

Hi, I have a small requirement where i need to sum up a column in a text file. Input file 66ab 000000 534385 -00000106350.00 66cd 000000 534485 -00013364511.00 66ad 000000 534485 -00000426548.00 672a 000000 534485 000000650339.82... (5 Replies)
Discussion started by: pssandeep
5 Replies

8. Shell Programming and Scripting

How to sum particular field in text file?

how to sum particular field in text file i am having text file like 222|4000|abc 333|5000|xyz 444|6000|mno i want sum of second field i.e 4000+5000+6000 can u pls help (3 Replies)
Discussion started by: suryanarayana
3 Replies

9. Shell Programming and Scripting

how to get the sum of all the lines in the file

Hi I have the following file, how I will calculate the sum of all the entries in the file. > cat abc 2 3 4 now the sum should be 2+3+4 = 9 (4 Replies)
Discussion started by: sdosanjh
4 Replies

10. Shell Programming and Scripting

calculating sum of fields in a file

Hey, I have a file and it has only one field. I need to calculate the sum of each filed as total. For e.g my file is 1 2 3 4 5 I need to calculate the total sum as 15. Please let me know how i can do it? (4 Replies)
Discussion started by: dsravan
4 Replies
Login or Register to Ask a Question