Quote:

>Hi,

>I have some files with 2 columns of numbers to reformat, representing

>the start and end positions of an object. All the numbers in the 1st

>column are sorted (increase order):

>column1 column2

>x(1) y(1)

>x(2) y(2)

>x(3) y(3)

>... ...

>x(n) y(n)

>x(n+1) y(n+1)

>... ...

>with x(n+1) > x(n)

>I want to compare iteratively all the lines 2 by 2 this way:

>if x(n+1) <= y(n) then replace the 2 lines (n) and (n+1) by a single

>line with min[x] in column1 and max[y] in column2. Is there any way to

>do that with awk ?

>Fab.

Hi Fabrice,

I wrote this awk program:-

NF==2 {

x[n+1] = $1

y[n+1] = $2

if (n > 0) {

if ( x[n+1] <= y[n] ) {

x[n] = min( x[n], x[n+1] )

y[n] = max( y[n], y[n+1] )

n--

}

}

n++

Quote:

}

END {

for (i=1; i<=n; i++) print x[i], y[i]

Quote:

}

function min(a,b) {return a<=b ? a : b}

function max(a,b) {return a>=b ? a : b}

I used this data file:-

1 2

3 5

4 6

6 8

9 10

10 11

I got these results:-

gawk -f fab.awk fab.dat

1 2

3 8

9 11

Hope this helps

--

Alan Linton