Dynamic calculation

Hi Experts,

please see below are the dynamic text files.

abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,200,city
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,220,basket

I need a shell script which can show me those lines which is having data greater than 200.
for example in the below line
abc,445,hhh,data,10,country,data,88,city
script should add the data value like 10+88=98 and if the value is greater than 200 it should print the whole line like
abc,445,hhh,data,10,country,data,88,city,games,100,data,220,basket
data =10+88+220 is greater than 200 so it should print the whole line.

Note the data value is dynamic it can appera many times in the file.

Thanks

Hi All,

Is there is any soloution using array.
please let me know

Thanks

Try this solution:

awk -F, 'NF>12 && ($5 + $8 + $13) > 199'  file

Hi,

Thanks for your solution but your command will only work when the number of fields are greater than 12 but as i have said in my case the number of fileds are dynamic some times 4 or 10 or 15 or 30 or 2 so in this case we need to consider the string "data". Means when ever data comes the script should take the next value after data and then add all the values after data and finally copare with 200. if its greater than 200 then it should show me the whole line.
how can i do that.

Thanks

 awk -F, ' {c=$0; for(i=3;i<=NF;i++)
           {if(($i~/[0-9]+/)&&($i!~/[^0-9]+/)) sum=sum+$i;
            if((i==NF) && (sum>200)) {print c; sum=0}}}' file

@rubin you should reset sum at the beginning and you don't need to hold the line, just print.

awk -F, '{sum=0;for(i=3;i++<=NF;){if(($i~/[0-9]+/)&&($i!~/[^0-9]+/)) sum+=$i;if((i==NF)&&(sum>200)) print}}' file

Not necessarily.

c=$0 holds just one line, and does not affect performance, ( or it's completely unnoticeable ).
And your modifications to the code are simply minor & cosmetic.

It would have been more beneficial to the OP & forum if you provided the right solution since your first post, while the OP was very clear in his initial post.

This is not a fight, just I want to say that I get different results, check below.

# cat file
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,200,city
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,220,basket

#  awk -F, ' {c=$0; for(i=3;i<=NF;i++)
>            {if(($i~/[0-9]+/)&&($i!~/[^0-9]+/)) sum=sum+$i;
>             if((i==NF) && (sum>200)) {print c; sum=0}}}' file
abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,200,city
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,220,basket

# awk -F, '{sum=0;for(i=3;i++<=NF;){if(($i~/[0-9]+/)&&($i!~/[^0-9]+/)) sum+=$i;if((i==NF)&&(sum>200)) print}}' file
abc,445,hhh,data,10,country,data,200,city
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,20,basket
abc,445,hhh,data,10,country,data,88,city,games,100,data,220,basket

... and I didn't understand the OP request the first time .

:slight_smile: Never meant to start one, but thanks for pointing the difference.

abc,445,hhh,data,10,country,data,88,city
abc,445,hhh,data,10,country,data,200,city

Hmm...strange how I missed it, and I tested it (or so I thought) quite a few times with a fancier sample. Kinda forgot the fact that the "sum" from the lines that don't satisfy the conditions, gets also carried over.

Not a problem, it can happen to anyone :).

Hi,
First of all i would like to thanks all of you who replied to my post and i really apprecate your kindl help. i have few questions related to you command.

awk -F, '{sum=0;for(i=3;i++<=NF;){if(($i~/[0-9]+/)&&($i!~/[^0-9]+/)) sum+=$i;if((i==NF)&&(sum>200)) print}}' file

1) in this command you are not considering the field having string data
2) i am new to shell scripting and finding my self difficult to understand this command so can you please explain the command like why you have started with i=3 and what does this command will do ;){if(($i~/[0-9]+/)&&($i!~/[^0-9]+/)) ?

Many Thanks

Use

 tags when you post code. 
 awk -F, -v v='data' '{s=0;for(i=3;i++<=NF;){if($i==int($i) && c==v) s+=$i;if((i==NF)&&(s>200)) print;c=$i}}' file