title: Is there a way to define a user-defined function inside an awk statement which is inside a bash script?
body: <p>I think the question speaks for itself. I am a beginner so please let me know if this is possible or not. If not, came you give me a better solution because my bash script depends heavily on certain <code>awk</code> statements but the majority of the script is in bash. I am comparing two files using an <code>awk</code> statement in my bash script. Basically checking if the values are the same or not. (see the files below)</p>
  
  <p>file1.txt </p>
  
  <pre><code>Name  Col1  Col2  Col3 
  -----------------------
  row1  1     4     7         
  row2  2     5     8          
  row3  3     6     9
  </code></pre>
  
  <p>file2.txt</p>
  
  <pre><code>Name  Col1  Col2  Col3   
  -----------------------         
  row1  1     4     7 
  row2  2     5     999
  </code></pre>
  
  <p>Here is the structure of my bash script:</p>
  
  <pre><code>#!/bin/bash
  .
  .
  .
  
  awk -F '\t' '
                # skips first two lines
                FNR &lt; 2 {next}                       
                FNR == NR {           
                  for (i = 2; i &lt;= NF; i++) {
                    a[i,$1] = $i;      
                  }    
                  b[$1];           
                  next;       
                }
  
                ($1 in b) {                          
  
                  for (i = 2; i &lt;= NF; i++) 
                  {
                     # does something
                  }
                }
  ' file1.txt file2.txt
  .
  .
  .
  </code></pre>
  
  <p>I have a lot of repetitive code that are only useful in the <code>awk</code> statements and so I want to create a function that would be placed in the <code>awk</code> statement to avoid repetition. Is this possible? </p>
  
  <p>Let me know if further explanation is required. </p>