title: Assign variable to awk '{if ...}' statement in while loop
body: <p>I am struggling to use a variable I set in a <code>while loop</code> for <code>awk</code>.</p>
  
  <p>For example, I have a file that contains this:</p>
  
  <pre><code>1-WW
  (2, 17.0)       (3, 25.0)       (4, 32.0)       (5, 37.0)       (6, 42.0)       (7, 46.0)       (8, 50.0)       (9, 53.0)       (10, 56.0)      (11, 59.0)      (12, 62.0)      (13, 65.0)   (14, 67.0)    (15, 70.0)       (16, 72.0)      (17, 74.0)      (18, 77.0)      (19, 79.0)      (20, 81.0)      (21, 83.0)      (22, 84.0)      (23, 86.0)      (24, 88.0)      (25, 89.0)      (26, 90.0)   (27, 90.0)
  5-Ferm
  (2, 16.0)       (3, 24.0)       (4, 29.0)       (5, 34.0)       (6, 37.0)       (7, 41.0)       (8, 43.0)       (9, 46.0)       (10, 48.0)      (11, 50.0)      (12, 52.0)      (13, 54.0)      (14, 56.0)      (15, 58.0)      (16, 59.0)
  </code></pre>
  
  <p>The aim of the loop is to:</p>
  
  <ol>
  <li>Take the line that starts with <code>"^("</code> and make it into a text file with two columns -DONE</li>
  <li>Sort file based on column two and assign the max value to variable called <code>max</code> -DONE</li>
  <li>Find rows where column 2 <code>== $max</code> and find the <code>min</code> values of column $1 and append to output -HELP!</li>
  </ol>
  
  <p>The loop I have made is as below. It is quite clunky as I am rather new to bash and still very much learning!
  I am struggling with calling my variable <code>$max</code> into the final <code>awk</code> line. any advice would be welcome!</p>
  
  <p>I am using a <code>while loop</code> as the actual file I will be using has mutliple lines like the example above and I want to do this process for each line that starts with <code>"^("</code> 
  I have tried many variations on assigning the variable into awk, following many of the answers to questions on here but I cant seem to get any to work!</p>
  
  <pre><code>#!/usr/bin/env bash
  
  # read through all lines and if the start with "(" do ...
  while IFS='\t' read -r LINE || [ -n "${LINE}" ]; do
          if [[ "${LINE}" == "("* ]]; then
                  echo "line: ${LINE}"; # print out line to make sure correct
                  echo "${LINE}" | xargs -n 2 | sed 's/(//g' | sed 's/)//g' | awk -F"," '$1=$1' OFS="\t" &gt; loop_test; # make all (,) into two columns and read into loop_test
                  cat loop_test;
                  max= sort -nrk2,2 loop_test | awk '{print $2}' | head -n 1 # sort by columns 2 and find largest value - input to variable max
                  echo $max # check variable max has been assinged
                  awk -v var="$max" '{ if ($2 == var) { print } }' loop_test | sort -nrk1,1 | tail -n 1 | awk '{print $1}' &gt;&gt; out # now find all rows where column 2 = $max and find lowest value of column 1 - send value to ouptut
          fi
  done &lt; test
  </code></pre>
  
  <p>The expected output should be a file called <code>out</code> like so:</p>
  
  <pre><code>26
  16
  </code></pre>
  
  <p>Thank you in advance for your help and suggestions :)</p>