• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

Regex Match

January 19, 2019

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

Search This Website

Subscribe Via Email

  • Home
  • About
  • Archives

Copyright © 2023