Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/Linux/Regex Match

January 19, 2019

Regex Match

Regex is really cool. You can do amazing things with it, but if you’re starting new, it’s quite daunting task. If you ever find one that works, you better hang on to it for dear life. Here’s one that I know is working. It reads data from a file, and compares and matches a variable to a certain pattern. In the example below, the variable I’m searching for, must contain a string with the following characters “server-w.” They also must be followed by at least one number or a set of numbers.

cat filename | while read LINE; do
  if [[ $LINE =~ server-w-*.[0-9] ]]; then
     echo "Do not backup $LINE"
  else
     echo "Backup $LINE"
  fi
done

cat filename | while read LINE; do if [[ $LINE =~ server-w-*.[0-9] ]]; then echo "Do not backup $LINE" else echo "Backup $LINE" fi done

If you need help with Regex, check out a couple of sites below that allows you to test your regex skills. Regex and RegExr.

Filed Under: Linux Tagged With: bash, matching, regex, regular expressions

Have content delivered to your mail. Subscribe below.

About Me

I'm Ulysses, a Cloud Engineer at Cardinal Health based in Columbus, Ohio. I’m a certified AWS Solutions Architect. This website is my way of documenting the things I have learned in the Cloud. When off the grid, I enjoy riding my electric skateboard. I have surfed, snowboarded and played the saxophone in the past. I hope you will find this site helpful. It's powered by WordPress and hosted in AWS LightSail.

  • Cloud
  • Linux
  • Git

Copyright © 2012–2021