Summing numbers after specific word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summing numbers after specific word
# 1  
Old 05-25-2011
Summing numbers after specific word

Hi all,
Looking for suggestions on a better way to sum numbers in a key value pair formated file. What I have works but seems really clunky to me. Any suggestions would be greatly appreciated.


Code:
cat test.txt | perl -ne 'm/(M=)(\d+\.?\d?\d?)/ && print "$2\n"' | awk '{ sum+=$1} END {printf "%18.2f\n",sum}'

Sample data (test.txt):
Code:
123 { id=111 M=12. ST=OR}
123 { id=112 M=12.0 ST=ID}
123 { id=113 M=12.22 ST=NM}
123 { id=114 M=12.00 ST=NY}
123 { id=115 M=12. ST=WA}
123 { id=116 M=12 ST=IA}
123 { id=117 M=22.54 ST=MA}

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-25-2011 at 05:26 PM.. Reason: code tags, please!
# 2  
Old 05-25-2011
And what is the desired output? Also explain in more detail how you want this sum to be calculated (which field, when etc).
# 3  
Old 05-25-2011
Code:
nawk -F'[= ]' '{s+=$6}END {printf("%18.2f\n", s)}' test.txt

# 4  
Old 05-25-2011
Sorry forgot to mention that the each line in the file can vary:
Code:
123 { id=111 X=123 M=12. ST=OR}
123 { id=112 X=123 y=123 M=12.0 ST=ID}
123 { id=113 Z=222 M=12.22 ST=NM}
123 { id=114 X=321 Y=1 Z=88 M=12.00 ST=NY}
123 { id=115 X=1 M=12. ST=WA}
123 { id=116 X=222 M=12 ST=IA}
123 { id=117 M=22.54 ST=MA}

I need it formatted in as 18.2 and need the M value included in the summary for each row as it will never be missing. Thanks again.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-25-2011 at 05:41 PM.. Reason: once again - please use code tags!
# 5  
Old 05-25-2011
Code:
nawk -F'[= ]' '{s+=$(NF-2)}END {printf("M=%18.2f\n", s)}' test.txt

OK, please provide the desired output based on your sample file - this is getting vague Smilie
# 6  
Old 05-25-2011
Sorry for being vague...was simply looking for the sum to be output with two decimal points so nothing fancy. This works great, thanks for your help. How would you implement this if the position of M wasn't the same going right to left?

Code:
123 { id=110 X=123 M=12. ST=OR XX=2}
123 { id=111 M=12. ST=OR}
123 { id=112 X=123 y=123 M=12.0 ST=ID R=4}
123 { id=113 Z=222 M=12.22 ST=NM QQ=2 L=3}

# 7  
Old 05-25-2011
It can be done in a single Perl line:
Code:
perl -ne '$s+=(/(?<=M=)[\d.]+/g)[0];END{printf "%18.2f\n",$s}' test.txt

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

2. Shell Programming and Scripting

Summing up values of rows of numbers

data file contains failed=24 error=23 error=163 failed=36 error=903 i need to get a total count of each value above. i'm looking for the most efficient method to do this as the datafile i provided is just a sample. the actual data can be several hundred thousands of lines. so from... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

5. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: &quot;Liberation Sans&quot;; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

6. Shell Programming and Scripting

awk summing specific lines and fields

Hi I would like to know if it is possible to sum some specific fields. I have this x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157 a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933 b;b;b;b;b;b;b;b;232,797,478,723... (5 Replies)
Discussion started by: nakaedu
5 Replies

7. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

8. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

9. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies

10. Shell Programming and Scripting

summing numbers in files

I am trying to take two files and add the numbers from each. There is a total of 5192 numbers in each file and I want to add them row by row... ie. first row of file 1 + first row of file 2 = first row of output. Eventually I will be summing 40401 of these files together but starting with 2 just... (21 Replies)
Discussion started by: pattywac
21 Replies
Login or Register to Ask a Question