can awk do that ? 
Author Message
 can awk do that ?

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.



Tue, 30 Dec 2003 15:06:12 GMT  
 can awk do that ?


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



Tue, 30 Dec 2003 18:47:32 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Can this be done in AWK?

2. How is this done in awk/nawk/gawk?

3. Can this be done with AWK or ED ?

4. CA Cans VO ?

5. It's not bad canned meat...

6. It's not bad canned meat...

7. It's not bad canned meat...

8. Using CGI module with 'canned queries'

9. It's not bad canned meat...

10. Doing assembly and really doing assembly

11. Doing assembly and really doing assembly

12. common mistakes in awk: comparing awk with C

 

 
Powered by phpBB® Forum Software