Home > Blockchain >  Multiple "grep...then..." in bash
Multiple "grep...then..." in bash

Time:12-31

I'm writing a bash script which monitoring the output of script A, and matching keyword by "grep" command. If successfully found the keyword, echo something. Here is the script I have:

if script_A | grep -q 'keyword'; 
then
echo 'Found A!'

The script function well if only one condition. However I cannot find a way to match several keywords, and using "if...elif...else" to control the echo content for different conditions.

Here is the logic I'm trying to achieve:

script_A |
if grep 'keyword_A';
then echo 'Found A!'
elif grep 'keyword_B';
then echo 'Found B!'
else echo 'Found Nothing!'

Thanks!

CodePudding user response:

Each write can have only one reader consume if. That reader can then create multiple copies of the data it read (like tee does), but there has to be just one thing on the other end initially.

A more conventional approach would be to have the shell be that one reader, as in:

output=$(script_A)
if grep -q 'keyword_A' <<<"$output"; then
  echo 'Found A!'
elif grep -q 'keyword_B' <<<"$output"; then
  echo 'Found B!'
else
  echo 'Found nothing!'
do

CodePudding user response:

If you are happy just seeing the output of grep (instead of your cusom messages), you can just do:

if ! script_A | grep -e 'keyword_A' -e 'keyword_B'; then
    echo 'Found Nothing'
fi

It's somewhat difficult to directly manipulate the output of grep in the pipeline, but you could get custom messages with:

if ! script_A | grep -o -e 'keyword_A' -e 'keyword_B'; then
    echo 'Nothing'
fi | sed -e 's/^/Found /' -e 's/$/!/'

CodePudding user response:

You can use the Bash rematch:

if [[ "demo_rematch" =~ [st] ]]; then
  echo "Matched from regexpr [st] is the letter ${BASH_REMATCH}!"
fi

With your single letter keywords you can do something like

# grep [YES] is the same as grep [ESY]
for regex in '[AB]' '[YES]' '[NO]'; do
  echo "$regex"
  if [[ "$(printf "keyword_%s\n" {A..G})" =~ keyword_$regex ]]; then
    echo "Found ${BASH_REMATCH/keyword_}!"
  else
    echo "Found Nothing!"
  fi
done

In real live your keywords might be more complicated. You can still use the same construction, but now I will not use the string "keyword_".

regex='(foo|bar|A|B|not me|no match|bingo)'
echo "$regex"
for script_A in "String with foos" "String with bar" "String with A" "String with B" "Nothing here" "Please ignore me" "and bingo"; do
  if [[ "${script_A}" =~ $regex ]]; then
    echo "Found ${BASH_REMATCH}!"
  else
    echo "Found Nothing!"
  fi
done
  •  Tags:  
  • bash
  • Related