Count the field values in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the field values in a file
# 1  
Old 05-15-2013
Count the field values in a file

Hi I have a file with contents like :

Code:
101,6789556897,0000795369 - seq[0152906817] - fmt_recs[187] - avg_recs[187] 
101,4678354769,0000835783 - seq[0000000397] - fmt_recs[98] - avg_recs[98] 
221,5679787008,0001344589 - seq[0000249687] - fmt_recs[1283] - avg_recs[1269]

I need to find the sum of the all the values (which are in bold).

here : my output should be sum of these values : 187,98,1283.


Can any one suggest me how to acheive this ?
Smilie
# 2  
Old 05-15-2013
try..

Code:
 
awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' filename

# 3  
Old 05-15-2013
Quote:
Originally Posted by vidyadhar85
try..

Code:
 
awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' filename

It didn't work Smilie
Code:
% more text.dat
101,6789556897,0000795369 - seq[0152906817] - fmt_recs[187] - avg_recs[187] 
101,4678354769,0000835783 - seq[0000000397] - fmt_recs[98] - avg_recs[98] 
221,5679787008,0001344589 - seq[0000249687] - fmt_recs[1283] - avg_recs[1269]
% awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' text.dat
Sum is: 0

# 4  
Old 05-15-2013
Its working fine on AIX atleast.. what OS you working on?

Code:
 
/home/> awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' v
Sum is: 1568
/home/> cat v
101,6789556897,0000795369 - seq[0152906817] - fmt_recs[187] - avg_recs[187]
101,4678354769,0000835783 - seq[0000000397] - fmt_recs[98] - avg_recs[98]
221,5679787008,0001344589 - seq[0000249687] - fmt_recs[1283] - avg_recs[1269]

# 5  
Old 05-15-2013
Works for me
Code:
awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' t
Sum is: 1568

What system are you trying this on?
What does this give you? awk -F"[][]" '{print $1"-"$2"-"$3"-"$4}'
# 6  
Old 05-15-2013
Quote:
Originally Posted by Jotne
Works for me
Code:
awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' t
Sum is: 1568

What system are you trying this on?
What does this give you? awk -F"[][]" '{print $1"-"$2"-"$3"-"$4}'
It gave this output:
Code:
awk -F"[][]" '{print $1"-"$2"-"$3"-"$4}' text.dat
101,6789556897,0000795369 - seq-0152906817] - fmt_recs-187] - avg_recs-187] 
101,4678354769,0000835783 - seq-0000000397] - fmt_recs-98] - avg_recs-98] 
221,5679787008,0001344589 - seq-0000249687] - fmt_recs-1283] - avg_recs-1269]

I'm working on solaris
# 7  
Old 05-15-2013
Seem that you have problem with filed separator.
Try this then
Code:
awk -F"\[|\]" '{a+=$4}END{print "Sum is: "a}' filename

or this
Code:
awk -F"[" '{a+=$3}END{print "Sum is: "a}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to look up values in File 2 from File 1, & printingNth field of File1 based value of File2 $2

I have two files which are the output of a multiple choice vocab test (60 separate questions) from 104 people (there are some missing responses) and the question list. I have the item list in one file (File1) Item,Stimulus,Choice1,Choice2,Choice3,Choice4,Correct... (5 Replies)
Discussion started by: samonl
5 Replies

2. Shell Programming and Scripting

Count null values in a file using awk

I have the following a.txt file A|1|2|3|4|5| A||2|3|0|| A|1|6||8|10| A|9|2|3|4|1| A|0|9|3|4|5| A||2|3|4|5| A|0|av|.9|4|9| I use the following command to count null values for 2nd field awk -F"|" '!$2 { N++; next } END {print N}' a.txt It should give the result 2, but it is giving... (2 Replies)
Discussion started by: RJG
2 Replies

3. Shell Programming and Scripting

End of each line count the values in the file

Hi, How to find the end of character in the file. My requirement should be as below.1 is repeating 1 time ,2 is repeating 3 times... type 1: 1 type 2: 3 type 3: 2 9f680177|20077337258|0|0|0|1000004647916|1 9f680177|20077337258|0|0|0|1000004647916|2 9f680177 20077337258 0 0 0... (5 Replies)
Discussion started by: bmk
5 Replies

4. UNIX for Dummies Questions & Answers

count values based on contents of another file

Hello, I have two files as shown below: test1 678 679 689 690 710 test2 1 678 654 800 676 791 689 900 I want to get a count of lines from test2 whose columns bound the values in test1 I tried running the code below; however am getting wrong results. (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

5. Shell Programming and Scripting

Read File and Display The Count of a particular field

Hi Mates, I require help in the following: I have the following file snmp.txt Wed Mar 2 16:02:39 SGT 2011 Class : mmTrapBladeS origin : 10.0.0.0 hostname : 10.0.0.2 msg : IBM Blade Alert: Calendar Index : 10.0.0.2-IBMBLADE Fri Mar 4 07:10:54 SGT 2011 Class : mmTrapBladeS... (2 Replies)
Discussion started by: dbashyam
2 Replies

6. Shell Programming and Scripting

How to count Unique Values from a file.

Hi I have the following info in a file - <Cell id="25D"/> <Cell id="26A"/> <Cell id="26B"/> <Cell id="26C"/> <Cell id="27A"/> <Cell id="27B"/> <Cell id="27C"/> <Cell id="28A"/> I would like to know how would you go about counting all... (4 Replies)
Discussion started by: Prega
4 Replies

7. Shell Programming and Scripting

Count the repetition of a Field in File

Hi, Thanks for keeping such a help-full platform active and live always. I am new to this forum and to unix also. Want to know how to count the repetition of a field in a file. Anything of awk, sed, perl, shell script, solution are welcomed. Input File------------------ abc,12345 pqr,51223... (10 Replies)
Discussion started by: indian.ace
10 Replies

8. Shell Programming and Scripting

Getting Distinct values from second field in a file....

Hi I have a pipe delimited file. I am trying to grab the DISTINCT value from the second field. The file is something like: 1233|apple|ron 1234|apple|elephant 1235|egg|man the output I am trying to get from second field is apple,egg (apple coming only once) Thanks simi (4 Replies)
Discussion started by: simi28
4 Replies

9. Shell Programming and Scripting

Count field frequency in a '|' delimited file

I have a large file with fields delimited by '|', and I want to run some analysis on it. What I want to do is count how many times each field is populated, or list the frequency of population for each field. I am in a Sun OS environment. Thanks, - CB (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

10. Shell Programming and Scripting

finding field count escaping the blank values

Hi All I have a file.Below are few records of the file. sample.txt CPS,ES,843232910001,ESF81462,W N LINDSAY LTD,01674840629,09-FEB-2009,23-FEB-2009,CDR735,ALL CALLS,01674840629 CPS,ES,843232670001,ESF81462,W N LINDSAY LTD,01674840629,09-FEB-2009,23-FEB-2009,CDR734,ALL... (2 Replies)
Discussion started by: king007
2 Replies
Login or Register to Ask a Question