Sum up values followed by pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sum up values followed by pattern
# 1  
Old 09-11-2014
Sum up values followed by pattern

I have a file with data merged from multiple files. File contains header, data and trailer of multiple files. The trailer starts with 99 and delimiter is ~.
Trailer
99~120
99~30

As it is a merged file we i have multiple lines followed by 99~. Need help to find sum of values which are there in front of '99~'.
# 2  
Old 09-11-2014
If "in front of" means "following" (as stated in the thread title):
Code:
awk -F'~' '($1=="99") { sum+=$2 } END { print sum+0 }' file

Looking for 99 left from a ~ and summing up the value right from the ~
At the end printing the sum.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 09-11-2014
I am reading the requirements very differently than MadeInGermany.

Please show us some sample input and the output you expect to get from that input.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-11-2014
Quote:
Originally Posted by MadeInGermany
If "in front of" means "following" (as stated in the thread title):
Code:
awk -F'~' '($1=="99") { sum+=$2 } END { print sum+0 }' file

Looking for 99 left from a ~ and summing up the value right from the ~
At the end printing the sum.
Thanks
Can we check for 99~ and then will sum up the value on right?
How can I store this Output of awk command to any other file?

---------- Post updated at 08:28 AM ---------- Previous update was at 08:14 AM ----------

Quote:
Originally Posted by Don Cragun
I am reading the requirements very differently than MadeInGermany.

Please show us some sample input and the output you expect to get from that input.
Hello Don,

The File available will be like this
Code:
01~124353~abcd~YES~Y~can
02~234566~wbcd~No~N~can
03~345666~pqrs~No Response~can
04~234356~zbcd~No~N~can
05~924353~mbcd~YES~Y~can
99~5
01~124311~fbcd~No Response~can
02~884566~nbcd~No~N~can
03~555666~wqrs~YES~Y~can
04~444356~xbcd~No~N~can
99~4

output should be : saved in other file or displayed on screen like this
Code:
Total Records : 263 
YES : 143 
NO : 8 
No Response : 102

Here,
Total Records : is sum of values on the right side of 99~
YES is occurrences of ~YES~ in file
NO is occurrences of ~NO~ in file
NO Response is occurrences of ~NO Response~ in file
# 5  
Old 09-12-2014
This discussion has been going on in another thread started earlier: Total count in each category for given file list

This thread is closed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sum the value with negative values?

Hi Gurus, I have requirement need to sum the value, the logic is if the value is negative then time -1, I tried below two ways. one is failed, another one doesn't work. awk -F"," '{if($8< 0 $8*-1 else $8) sum+=$8}{print sum, $8} END{printf("%.2f\n",sum)}' awk -F","... (4 Replies)
Discussion started by: ken6503
4 Replies

2. Shell Programming and Scripting

Need help in finding sum for values in 2 different fields

Hi there, I have 2 files in following format cat file_1 Storage Group Name: aaaa HBA UID SP Name SPPort ------- ------- ------ 0 21 Storage Group Name: bbbb HBA UID... (2 Replies)
Discussion started by: jpkumar10
2 Replies

3. UNIX for Dummies Questions & Answers

sum values based on ID

Hi, I would like to be able to sum up the counts of a column by the ID of another column. Example (although the actual file I have has thousands of IDs): Input file: A1BG-AS1:001 3 A1BG-AS1:002 0 A1BG-AS1:003 2 A1CF:001 1038 A1CF:002 105 A1CF:003 115 A1CF:004 137 Desired output... (3 Replies)
Discussion started by: fadista
3 Replies

4. Shell Programming and Scripting

How do I find the sum of values from two arrays?

Hi I have redc containing the values 3, 6, 2, 8, and 1. I have work containing the values 8, 2, 11, 7, and 9. Is there a way to find the sum of redc and work? I need to compare the sum of those two arrays to something else, so is it okay to put that into my END? TY! (4 Replies)
Discussion started by: razrnaga
4 Replies

5. Shell Programming and Scripting

Getting a sum of column values

I have a file in the following layout: 201008005946873001846130058030701006131840000000000000000000 201008006784994001154259058033001009527844000000000000000000 201008007323067002418095058034801002418095000000000000000000 201008007697126001722141058029101002214158000000000000000000... (2 Replies)
Discussion started by: jclanc8
2 Replies

6. Shell Programming and Scripting

How to sum up two decimal values?

I am running the following script : cat ind_sls_extr_UX.out_sorted | while read each_rec do count=`echo "${each_rec}" | cut -c1-2` if then final_amount=0 amount=`echo "${each_rec}" | cut -c280-287` echo "${amount}" final_amount=`expr ${amount} + ${amount}` ... (7 Replies)
Discussion started by: mady135
7 Replies

7. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

8. Shell Programming and Scripting

How to sum values from top

Hi. Im looking for way to sum numbers from top. For example i have such command top -b -n | grep Cpu | cut -c 35 - 39 which give me output 97.0 . Ho can i do with that value any arithmetic actions (for example 97.0 +1)? Using c = $((top -b -n | grep Cpu | cut -c 35 - 39)) gives me... (8 Replies)
Discussion started by: qdf
8 Replies

9. Shell Programming and Scripting

how to sum values from 2 different files?

Hi I am trying to add count values from two different files into one file. Could any body please suggest me best command to do this? My problem was as follows: a.txt b.txt c.txt 10 20 30(needed) i tried cat a.txt b.txt > c.txt (its not adding the values) Thanks in advance.. Praveen (8 Replies)
Discussion started by: npk2210
8 Replies

10. Shell Programming and Scripting

How to sum column 1 values

I have a file file like this. I want to sum all column 1 values. input A 2 A 3 A 4 B 4 B 2 Out put A 9 B 6 (3 Replies)
Discussion started by: suresh3566
3 Replies
Login or Register to Ask a Question