Search code examples
linuxawkcsh

Find largest/smallest of two numbers


I have a csh script in which I access several header files to extract xmin, ymin co-ordinates of various bounding boxes. Is there a simple way of finding minx, maxx, miny maxy from an echo piped to something such as awk. Eg

set minx=`echo $x1\t$x2 | awk {something}`

set maxx = `echo $x1\t$x2 | awk {something else}

I doubt awk is the best way to go and am unsure what is. Here is a bit of my code:

   set bb1 = `label_file -g "bounding box[0]" r$start_roi`

@ bb1_x = $bb1[1]
@ bb1_y = 1023 - $bb1[4]
@ bb1_h = $bb1[4] - $bb1[2]
@ bb1_w = $bb1[3] - $bb1[1]

This will return the xmin, ymin width and height of a rectangular region of interest. I will tweak and repeat the above code to find the same parameters of a second region.

What I would then like to do is find the global xmin, ymin, xmax and ymax in order to define a larger rectangle that completely encompasses the smaller 2.

i.e set xmin to be the smaller number from $bb1[1] and $bb2[1] set xmax to be the larger from $bb1[3] and $bb2[3]

etc

thanks


Solution

  • This will print the min and max values given a pipe or file or numbers:

    awk '{
       min = ($0 =< min ? $0 : min)
       max = ($0 >= max ? $0 : max)
    }
    END {
       print min, max
    }'
    

    If that's not what you want, provide some sample input and expected output.

    You are almost certainly approaching your larger task in completely the wrong way by parsing files in (c)shell and invoking awk to do arithmetic. awk was designed to parse text files.

    EDIT: based on this code segment from your updated question:

    set bb1 = `label_file -g "bounding box[0]" r$start_roi`
    @ bb1_x = $bb1[1]
    @ bb1_y = 1023 - $bb1[4]
    @ bb1_h = $bb1[4] - $bb1[2]
    @ bb1_w = $bb1[3] - $bb1[1]
    

    and assuming "label_file" is some command that outputs 4 space-separated numbers, you could do that as:

    label_file -g "bounding box[0]" "r$start_roi" |
    awk '{
       bb1_x = $1
       bb1_y = 1023 - $4
       bb1_h = $4 - $2
       bb1_w = $3 - $1
    }'
    

    You say you have a second region too. Let's assume "$start_roi" is the argument for label_file that changes. Then you could do something like:

    ( label_file -g "bounding box[0]" "r$start_roi";
      label_file -g "bounding box[0]" "r$other_roi" ) |
    awk '{
       bb1_x[NR] = $1
       bb1_y[NR] = 1023 - $4
       bb1_h[NR] = $4 - $2
       bb1_w[NR] = $3 - $1
    }'
    

    and at that point bbl_x[1] will hold the X values for the start_roi box while bbl_x[2] will hold the X values for the other_roi box. You can see that with:

    ( label_file -g "bounding box[0]" "r$start_roi";
      label_file -g "bounding box[0]" "r$other_roi" ) |
    awk '{
       bb1_x[NR] = $1
       bb1_y[NR] = 1023 - $4
       bb1_h[NR] = $4 - $2
       bb1_w[NR] = $3 - $1
    }
    END {
       for (i=1; i<=NR; i++) {
          print i, bbl_x[i], bbl_y[i], bbl_h[i], bbl_w[i]
       }
    }'
    

    If you need help figuring out what to do from there, let us know.