title: awk: function to escape regex operators from a string
body: <p>Need a function to escape a string containing regex expression operators in an awk script.</p>
  
  <p>I came across this 'ugly' solution:</p>
  
  <pre><code>function escape_string( str )
  {
      gsub( /\\/, "\\\\",  str );
      gsub( /\./, "\\.", str );
      gsub( /\^/, "\\^", str );
      gsub( /\$/, "\\$", str );
      gsub( /\*/, "\\*", str );
      gsub( /\+/, "\\+", str );
      gsub( /\?/, "\\?", str );
      gsub( /\(/, "\\(", str );
      gsub( /\)/, "\\)", str );
      gsub( /\[/, "\\[", str );
      gsub( /\]/, "\\]", str );
      gsub( /\{/, "\\{", str );
      gsub( /\}/, "\\}", str );
      gsub( /\|/, "\\|", str );
  
      return str;
  }
  </code></pre>
  
  <p>Any better ideas?</p>