0

I have a file that may contain the following block of code in any one of the combinations. I need to check for the presence of "import telemetry" in the file.

  1. import telemetry { prefix tm; }
  2. import     telemetry { prefix tm; }
  3. import

    telemetry { prefix tm; }

My code:

#!/bin/sh

if grep -Eq "import\s+telemetry"  "./xyz.yang";
    then
      echo "This is a telemetry yang"
    else
      echo "This is a normal yang"
fi

The above works for 1 and 2 but not for 3.

I tried following but it's greedy and will match "import blah blah blah.. telemetry " as well.

if awk '/import/,/telemetry/' "./xyz.yang";

Any suggestions?

My solution:

#!/bin/sh
    if grep -Pzq 'import[ \n\r\t]+telemetry' './xyz.yang';
        then
          echo "This is a telemetry yang"
        else
          echo "This is a normal yang"
    fi
void
  • 338
  • 5
  • 19

1 Answers1

-1

Add a -z flag to grep and it will work.

#!/bin/sh

if grep -Eqz "import\s+telemetry"  "./xyz.yang";
    then
      echo "This is a telemetry yang"
    else
      echo "This is a normal yang"
fi
samthegolden
  • 1,366
  • 1
  • 10
  • 26