awk to print mon and max values of ranges


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print mon and max values of ranges
# 1  
Old 02-02-2006
awk to print mon and max values of ranges

HI all

I'm trying to write an awk script to print the min and max value in a range(s) contained in another file - the range values are in $2
EG
114,7964,1,y,y,n
114,7965,1,y,y,n
114,7966,1,y,y,n
114,7967,1,y,y,n
114,7969,1,y,y,n
114,7970,1,y,y,n
114,7971,1,y,y,n
114,7972,1,y,y,n
114,7973,1,y,y,n
114,7974,1,y,y,n
114,7977,1,y,y,n
114,7980,1,y,y,n
114,7981,1,y,y,n
114,7983,1,y,y,n

The output should produce
7964
7967
7969
7974
7977
7977
7980
7983

Or ideally
7964 - 7967
7969 - 7974
7977
7980 - 7983

SO far I have this awk script
BEGIN{
FS=OFS=","
nxt=0
}
$2!=nxt {print $2;tmp=$2;nxt=int($2+1)}
Which produces the wrong max figure
i.e.
7964
7966
7969
7971
7973
etc .....

Note the extra 7971 ??

Any advice ??

thanks
# 2  
Old 02-02-2006
I'm not sure if your posted 'desired' output is what you actually want based on on explanation:
Code:
7964 - 7967
7969 - 7974
7977
7980 - 7983

The '7980 - 7983' has a 'hole' in the range - 1982.
Here's something to try:

nawk -f mud.awk myFile

mud.awk:
Code:
BEGIN {
  FS=OFS=","
}
!beg { beg=prev=int($2);next}
$2 != prev+1 { printf("%d - %d\n", beg, prev); beg=prev=last=$2;next}
{prev=last=$2}

END {
  printf("%d - %d\n", beg, last)
}

# 3  
Old 02-02-2006
Sorry - typo.
I've tried a different approach since I posted --

BEGIN{
FS=OFS=","
nxt=0
}
{
if (( $2 != nxt ))
{print tmp;print $2 "-";tmp=$2;nxt=int($2+1)}
else
{tmp=$2;nxt=int($2+1)}
}
END{
print tmp
}

Which basically works
Thanks again vgersh
# 4  
Old 02-02-2006
Your solution is much better

Thanks Again for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Derivation of values falling on date ranges

Hi Guys, I am having below tables used in oracle bal ID BALANCE BAL_DATE 1 -11.71 01-JAN-05 00.00.00 1 -405.71 02-JAN-05 00.00.00 1 -760.71 03-JAN-05 00.00.00 ref_table PRODUCT EFF_FROM_DATE EFF_TO_DATE TYPE MIN_AMT MAX_AMT CHARGE 12 01-JAN-05 00.00.00 01-JAN-06... (6 Replies)
Discussion started by: rohit_shinez
6 Replies

2. Shell Programming and Scripting

Print root number between min and max ranges

Hi to all, Please help on the following problem, I'm not where to begin, if awk or shell script. I have pairs of ranges of numbers and I need to find the root or roots of ranges based on min Range and Max ranges Example #1: If min range is 120000 and max ranges 124999, it means that are... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

3. Shell Programming and Scripting

How to get min and max values using awk?

Hi, I need your kind help to get min and max values from file based on value in $5 . File1 SP12.3 stc 2240806 2240808 + ID1_N003 ID2_N003T0 SP12.3 sto 2241682 2241684 + ID1_N003 ID2_N003T0 SP12.3 XE 2239943 2240011 + ID1_N003 ID2_N003T0 SP12.3 XE 2240077 2241254 + ID1_N003 ... (12 Replies)
Discussion started by: redse171
12 Replies

4. Shell Programming and Scripting

Print numbers between two number ranges

Hi, I have a list.txt file with number ranges and want to print/save new all.txt file with all the numbers and between the numbers. == list.txt == 65936 65938 65942 && 65943 65945 ... (7 Replies)
Discussion started by: AK47
7 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

6. Shell Programming and Scripting

print max number of 2 columns - awk

Is it possible to print max number of 2 columns - awk note: print max if the integer is positive and print min if the integer is negative input a 1 2 b 3 4 c 5 1 d -3 -5 d -5 -3 output a 2 b 4 c 5 d -5 d -5 (4 Replies)
Discussion started by: quincyjones
4 Replies

7. Shell Programming and Scripting

Values between ranges

Hi, I have two files file1 chr1_22450_22500 chr2_12300_12350 chr1_34500_34550 file2 11000_13000 15000_19000 33000_44000 If the file 1 ranges fall between file2 ranges then assign the value of file2 in column 2 to file1 output: chr2_12300_12350 11000_13000 chr1_34500_34550 ... (7 Replies)
Discussion started by: Diya123
7 Replies

8. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

9. Shell Programming and Scripting

Search values between ranges in File1 within File2

Hi people, I have 2 files, one with a list of non consecutive ranges (File1.txt), where each range begins with the value in column 1 and finishes with the value in column 2 in the same line, as can be seen above. 215312581156279 215312581166279 215312582342558 215312582357758... (4 Replies)
Discussion started by: cgkmal
4 Replies

10. UNIX for Dummies Questions & Answers

Awk search for max and min field values

hi, i have an awk script and I managed to figure out how to search the max value but Im having difficulty in searching for the min field value. BEGIN {FS=","; max=0} NF == 7 {if (max < $6) max = $6;} END { print man, min} where $6 is the column of a field separated by a comma (3 Replies)
Discussion started by: Kirichiko
3 Replies
Login or Register to Ask a Question