Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I execute an AWK statement in a Perl script which uses a Perl variable
    text
    copied!<p>I'm trying to use an awk statement inside a perl script which takes user input and searches though a number of text files to find lines which match all the words in the input in any order. To that end, I'm able to do the Awk search I want on the CLI thusly:</p> <pre><code>awk 'tolower($0) ~ / 204/ &amp;&amp; / test/ &amp;&amp; / leg/' *_Codes.txt </code></pre> <p>This will return lines in the referenced text files which include words starting with '204', 'test' and 'leg', such as 'left legs being tested in room 2045';</p> <p>When I try to do this in a Perl script, however, setting the user input to to a variable and modifying it to include the <code>&amp;&amp;</code> operators and slashes, I'm not getting anything back. Here's what I have:</p> <pre><code>my ($code_search, $code_set) = @_; # Clean the input for awk # trim whitespace from the ends $code_search =~ s!(^\s+|\s+$)!!g; # separate words with the &amp;&amp; operator and slashes $code_search =~ s!\s+!/ &amp;&amp; / !g; # make input lower case and tack on front and back slashes my $sanitized_query = lc "/ ${code_search}/"; # at this point, a user input of '204 leg test' # is transformed to '/ 204/ &amp;&amp; / leg/ &amp;&amp; / test/' # and is saved to the $sanitized_query variable # run the query through awk and save it to $results my $results = `awk 'tolower($0) ~ \$sanitized_query' *_Codes.txt`; </code></pre> <p>But <code>$results</code> doesn't give me anything.</p> <p>Maybe awk isn't the right tool for the job here, but it seems better suited for my needs than grep is, as I want to make sure I can search for all the terms entered and return results where they all appear in a line of text in any order.</p> <p>Any assistance is much appreciated. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload