bash script range calculations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script range calculations
# 1  
Old 02-26-2011
bash script range calculations

Hi,

I have data in the following form:

AB001 10
AB002 9
AB003 9
etc
AB200 5

What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure it should be fairly straight forward awk one liner, but am just having a mental block trying to work it out!

Anyone?
# 2  
Old 02-26-2011
Could this help you ?
Code:
awk 'BEGIN{g1=30;g2=50;g3=70;g4=90;g5=100}
{if(NR<=g1){gr1_total+=$2}
if(NR>g1&&NR<=g2){gr2_total+=$2}
if(NR>g2&&NR<=g3){gr3_total+=$2}
if(NR>g3&&NR<=g4){gr4_total+=$2}
if(NR>g4&&NR<=g5){gr5_total+=$2}
}
END {
print "GROUP 1:- "gr1_total
print "GROUP 2:- "gr2_total
print "GROUP 3:- "gr3_total
print "GROUP 4:- "gr4_total
print "GROUP 5:- "gr5_total
}' infile

# 3  
Old 02-26-2011
Code:
# range of 5
  nawk '{gsub("[^0-9]","",$1);a[int($1/r)]+=$2}END{for(i in a) print "Range " i ":",a[i]}' r=5 myFile | sort -k1.1
#range of 20
  nawk '{gsub("[^0-9]","",$1);a[int($1/r)]+=$2}END{for(i in a) print "Range " i ":",a[i]}' r=20  myFile | sort -k1.1

# 4  
Old 02-26-2011
Thanks both of you for the replies - I've used Pravin27's suggestion and works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script, find the next closed (not in use) port from some port range.

hi, i would like to create a bash script that check which port in my Linux server are closed (not in use) from a specific range, port range (3000-3010). the print output need to be only 1 port, and it will be nice if the output will be saved as a variable or in same file. my code is: ... (2 Replies)
Discussion started by: yossi
2 Replies

2. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

3. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
Discussion started by: LaurenRose
3 Replies

4. Shell Programming and Scripting

Arithmetic calculations in bash file

I have 2 numbers xmin = 0.369000018 xmax = 0.569000006 and want to calculate (xmax- xmin) / 5.0 I have tried using $(( )) but is always giving an error (8 Replies)
Discussion started by: kristinu
8 Replies

5. Shell Programming and Scripting

Date Calculations using script!!

Hi all, Thanks in Advance , i am very new to programming part in script i think using some caluations+ sed command only we can do this process in script. for exampl: i have file in that one line is like this using sed i can replace the date and all but my requirement is The... (3 Replies)
Discussion started by: anishkumarv
3 Replies

6. Shell Programming and Scripting

Bash script to test IP range on server

Hello, We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately. I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Discussion started by: boxgoboom
4 Replies

7. Shell Programming and Scripting

calculations in bash

HI i have following problem, i need to use split command to split files each should be cca 700 lines but i dont know how to inplement it in the scripts becasuse each time the origin file will be various size , any body got any idea cheers (2 Replies)
Discussion started by: kvok
2 Replies

8. Shell Programming and Scripting

Bash Script w/ IP Range

Is there a basic IP range script or a site that has basic script written? I am making a script that is looking for a device on my network threw HTTP://. I and using a for loop with wget to download the page and using grep to search the code for the serial number. My range is 172.16.x.x/16 I... (3 Replies)
Discussion started by: captaindoogles
3 Replies

9. Shell Programming and Scripting

any way to speed up calculations in bash script

hi i have a script that is taking the difference of multiple columns in a file from a value from a single row..so far i have a loop to do that.. all the data is floating point..fin has the difference between array1 and array2..array1 has 700 x 300= 210000 values and array2 has 700 values.. ... (11 Replies)
Discussion started by: npatwardhan
11 Replies

10. Shell Programming and Scripting

Non-integer calculations in bash

I'm new at scripting but I thought I was getting pretty good at it. I've hit a snag. I try to use expr to compute a fraction say: expr 3 / 4, and I'm getting zero. I guess it's just truncating to the integer, in this case 0, but I need the decimal 0.75. What can I do to compute this value in... (2 Replies)
Discussion started by: jeriryan87
2 Replies
Login or Register to Ask a Question