1

I have the following bash script segment that uses grep to find out whether input line contains certain pattern or not.

echo $input_line | grep -q "$pattern"
if [ $? -eq 0 ];
then
    # Input line contains pattern
    # Execute some bash commands
else
    # Input line DOES NOT contain pattern
    # Execute other bash commands
fi

I need to rewrite it with awk without grep. I tried several variants, but none of them works, for example, both of the following always print MATCH, no matter contains input line pattern or not.

echo $input_line | awk -v b="$pattern" '/pattern/ { print "MATCH" }'

echo $input_line | awk -v b="$pattern" '/$0 ~ pattern/ { print "MATCH" }'

Update:

Removing / as recommended resolved the described issue. Now I have the problem with returning result of awk command for usage in if statement. I wrote the following "ugly" snippet:

pattern="abc"  
input_line="xyz123 678q we uqa abc asd"  
RES=$(echo $input_line | awk -v pat="$pattern" '$0 ~ pat { print "1" }')  
if [ $RES -eq 1 ];  
then  
    echo Input line $input_line contains pattern $pattern  
else  
    echo Input line $input_line DOES NOT contains pattern $pattern  
fi  

input_line="tult uil7665 5444tu l098 7tu"  
RES=$(echo $input_line | awk -v pat="$pattern" '$0 ~ pat { print "1" }')  
if [ $RES -eq 1 ];  
then  
    echo Input line $input_line contains pattern $pattern  
else  
    echo Input line $input_line DOES NOT contains pattern $pattern  
fi  

So I've got the following output, because in second case when input line doesn't contain pattern the variable RES is not set:

Input line xyz123 678q we uqa abc asd contains pattern abc  
./test.sh: line 21: [: -eq: unary operator expected  
Input line tult uil7665 5444tu l098 7tu DOES NOT contains pattern abc  

Any ideas how to improve this "ugly" script snippet with if statement?
Thanks

Steeve007
  • 71
  • 1
  • 2
  • 5

1 Answers1

1

To use awk with a referenced variable for the pattern match you would need to do:

if [[ "$(input_line | awk '/'$pattern'/ { print "MATCH" }')" == "MATCH" ]]
then
         pattern matches
else
         No pattern matches
fi

I'm assuming here that the regular expression is a dynamic variable referenced by the variable pattern.

Place particular attention to the position of the single quotes in the awk statement.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • Never do this, it's wrong in multiple fundamental shell and awk ways. – Ed Morton Jul 09 '17 at 16:41
  • If you care to explain? – Raman Sailopal Jul 09 '17 at 21:07
  • 3
    A couple of the things you are doing that you should never do are not quoting a shell variable and letting the value of a shell variable expand to become part pf the text of an awk script. See any shell intro text and http://cfajohnson.com/shell/cus-faq-2.html#Q24, or google. You're now additionally trying to execute a variable and so will instead pick up any command of the same name in your PATH. You're also using awk to to a regexp match that bash could do itself, and generating text from awk to test as a comparison when you should just use an exit status. The OPs code was closer to correct. – Ed Morton Jul 09 '17 at 23:48
  • nice and clean solution – mati kepa Jul 27 '21 at 07:09