Dynamic calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic calculation
# 8  
Old 11-03-2008
This is not a fight, just I want to say that I get different results, check below.
Code:
# 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 .
# 9  
Old 11-03-2008
Quote:
Originally Posted by danmero
This is not a fight, just I want to say that I get different results,...
Smilie Never meant to start one, but thanks for pointing the difference.

Code:
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.

Quote:
... and I didn't understand the OP request the first time .
Not a problem, it can happen to anyone Smilie.
# 10  
Old 11-04-2008
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++<=NFSmilie{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 Smilie{if(($i~/[0-9]+/)&&($i!~/[^0-9]+/)) ?

Many Thanks
# 11  
Old 11-04-2008
Use [code] tags when you post code.
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

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