0

I have a text file which contains like below:

{"userId":"f1fcab","count":"3","type":"Stack"}
{"userId":"fcab","count":"2","type":"Stack"}
{"userId":"abcd","count":"5","type":"Stack"}

I want to get sum of the value of count.
I am using awk to achive this like below:

$ awk -F "," '{print $4}' test.txt    

How can I get only the integer type using awk and add them all.

My script should give me as

sum=10
1
  • awk 'match($0,"count\":\"([^\"])",a){x+=a[1]}END{print "sum="x}' file Commented Nov 25, 2014 at 13:08

4 Answers 4

2

You could try the below,

$ awk -F'"' '{sum = sum + $8;}END{print "sum="sum+0}' file
sum=10
  • -F'"' Sets the double quotes as FS value. Awk splits the row into colunms according to the value of FS variable.
  • sum = sum + $8 Calculate the sum of all the values in column no 8 and store it into a variable called sum
  • Finally by printing the variable sum at the end will give you the desired output.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Avinash..What is the use of $8
note you can say sum+=$8 instead of sum = sum + $8. Also, it is sometimes handy to print sum + 0, so that if sum wasn't set, you'll get a 0.
@fedorqui sum+0 is not needed as + $8 will always evaluate to an number even if there aren't 8 fields.If the field isn't a number or doesn't exist it is set to 0.
@Jidder interesting, good to know! I tested and you are right. Then, the edge case that +0 solves is when the sum wasn't executed at all. That is, awk '{sum+=$2} END {print sum}' file will fail if the file is empty; also, if the sum is within a condition that is never matched.
@fedorqui suppose, don't know why you'd want to do it on an empty file though. You can also just do +sum instead although i'm not sure if this is supported by all awks.
2

You can get the value of count key using double quotes (") as delimiter so that the eighth column will be the value to count on:

$ awk -F"\"" 'BEGIN {sum=0} {sum+=$8} END {print sum}' fd
10

2 Comments

Just one suggestion, " is a double quote.
BEGIN block is not needed, since sum is nothing at start and does not need to be declared. So this should do: awk -F\" '{sum+=$8} END {print sum}'. And then it would just be the same as paxdiablo posted.
1

Assuming consistent use of double quote characters, you can use:

awk -F\" '{s += $8} END{print "sum=" s+0}' inputFile

This will generate:

sum=10

This works because a quote delimiter gives you the fields:

1 2      3 4      5 6     7 8 ...
{"userId":"f1fcab","count":"3","type":"Stack"}

Comments

1
awk -F'[:"]' '{sum+=$10} END{print "sum=" sum}' File

Setting ':' and '"' as delimiters. Then taking the 10th field, which is the count value. add then up to sum and print at the end.

Example:

sdlcb@ubuntu:~/AMD_C/SO$ cat File
{"userId":"f1fcab","count":"3","type":"Stack"}
{"userId":"fcab","count":"2","type":"Stack"}
{"userId":"abcd","count":"5","type":"Stack"}
sdlcb@ubuntu:~/AMD_C/SO$ awk -F'[:"]' '{sum+=$10} END{print "sum=" sum}' File
sum=10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.