Dynamic calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic calculation
# 1  
Old 11-02-2008
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
# 2  
Old 11-02-2008
Hi All,

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

Thanks
# 3  
Old 11-02-2008
Try this solution:
Code:
awk -F, 'NF>12 && ($5 + $8 + $13) > 199'  file

# 4  
Old 11-03-2008
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
# 5  
Old 11-03-2008
Code:
 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

# 6  
Old 11-03-2008
@rubin you should reset sum at the beginning and you don't need to hold the line, just print.
Code:
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


Last edited by danmero; 11-03-2008 at 01:38 PM..
# 7  
Old 11-03-2008
Quote:
Originally Posted by danmero
@rubin you should reset sum at the beginning
Not necessarily.

Quote:
Originally Posted by danmero
and you don't need to hold the line, just print.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculation

Hi, i have a large file like this: Contig1 1 5 Contig1 2 4 Contig1 3 3 Contig1 4 5 Contig1 5 3 Contig1 6 4 Contig2 1 3 Contig2 2 7 Contig2 3 2 Contig2 4 9 Contig2 5 10 Contig2 6 3 Contig2 7 7 Contig2 8 2 Contig2 9 7 Contig2 10 5 contig1 2 4 contig1 3 3 contig1 4 5 (3 Replies)
Discussion started by: the_simpsons
3 Replies

2. Shell Programming and Scripting

Size calculation MB to GB

pcmpath query device |awk 'BEGIN{print "TYPE\tDEVICE NAME\tSERIAL\tSIZE\tHOSTNAME"} /DEVICE/ { disk=$5 printf "%s\t", $7 printf "%s\t", disk getline; printf "%s\t", substr($2, length($2)-3) ("bootinfo -s " disk) | getline; printf... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

3. Shell Programming and Scripting

VG calculation in GB

for i in `lsvg` do echo "VG Name:" $i echo "Total VG Size:" lsvg $i |grep "TOTAL PPs:" |awk '{print $7}' | cut -c2- echo "Free VG Size:" lsvg $i |grep "FREE PPs:" | awk '{print $7}' | cut -c2- done The PP Sizes are in MB. I like to have the sizes in GB. Also, I like to have the... (14 Replies)
Discussion started by: Daniel Gate
14 Replies

4. Shell Programming and Scripting

calculation

Could someone till me what this calculation really means let foo=`date "+(1%H-106)*60+1%M-100"` bar=foo+1440 (4 Replies)
Discussion started by: freddie999
4 Replies

5. Shell Programming and Scripting

duration calculation

I have a file which has 3 coloumns emp_name, Joining_date, Designation. abc 12/1/2001 SSE def 2/25/2007 SE ghi 3/18/2009 SA abc 8/1/2008 SSE def 2/13/2007 SE ghi 3/24/2005 SA I need to find out the emp who has been in the company for longest period(Till date). Can I have any... (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

6. UNIX for Advanced & Expert Users

Sql dynamic table / dynamic inserts

I have a file that reads File (X.txt) Contents of record 1: rdrDESTINATION_ADDRESS (String) "91 971502573813" rdrDESTINATION_IMSI (String) "000000000000000" rdrORIGINATING_ADDRESS (String) "d0 movies" rdrORIGINATING_IMSI (String) "000000000000000" rdrTRAFFIC_EVENT_TIME... (0 Replies)
Discussion started by: magedfawzy
0 Replies

7. Shell Programming and Scripting

summery calculation

Hi All I want to make summery for Date=245Duration=545 ... (1 Reply)
Discussion started by: nalakaatslt
1 Replies

8. Shell Programming and Scripting

decimal calculation

Hi am calculating the percentage of the pass and fail. Pass: echo `echo 1498*100/1667| bc`% fail: echo `echo 169*100/1667 | bc`% for this am getting PASS= 89% fail =10 % I need to fetch the exact decimal percentage like PASS = 89.8 % FAIL= 10.2 % Please advice me (3 Replies)
Discussion started by: bobprabhu
3 Replies

9. Shell Programming and Scripting

need help in decimel calculation

hi $lp_count = 25265531 $num_reads = 25280826 result=`echo "scale=2; $lp_count/$num_reads * 100" | bc` echo $result ------------------------------------------------------ o/p: 99.00 ------------------------------------------------------ desired: 99.93 (2 Replies)
Discussion started by: ali560045
2 Replies

10. Shell Programming and Scripting

calculation

Hi, I am in ksh88 I am trying to get the result of the calculation using 3 variables: TOTAL CAPACITY and get the following error: $DB_CAPACITY=(( $DB_SIZE * 100 / $TOTAL )) ksh: syntax error: `((' unexpected I cannot figure out what am I doing wrong... Thanks for any help -A (2 Replies)
Discussion started by: aoussenko
2 Replies
Login or Register to Ask a Question