Home > database >  using regex to iterate over files that matches a certain pattern in bash scripts
using regex to iterate over files that matches a certain pattern in bash scripts

Time:05-20

I have a regrex pattern ([0-9]*\.) [0-9]*_to_([0-9]*\.) [0-9]*, a matching string would be 11.1.1.1_to_21.1.1.1. I want to discover all files under a directory with the above pattern.

However I am not able to get it correctly using the code below. I tried to escape ( and ) by adding \ before them, but that did not work.

dir=$SCRIPT_PATH/oaa_partition/upgrade/([0-9]*\.) [0-9]*_to_([0-9]*\.) [0-9]*.sql 
for FILE in $dir; do
   echo $FILE
done

I was only able to something like this

dir=$SCRIPT_PATH/oaa_partition/upgrade/[0-9]*_to_*.sql
for FILE in $dir; do
   echo $FILE
done

Need some help on how to use the full regrex pattern ([0-9]*\.) [0-9]*_to_([0-9]*\.) [0-9]* here.

CodePudding user response:

Your regex is simple enough for replacing it bash extglob

#!/bin/bash
shopt -s extglob

glob=' (*([0-9]).)*([0-9])_to_ (*([0-9]).)*([0-9]).sql'

for file in "$SCRIPT_PATH"/oaa_partition/upgrade/$glob
do 
    printf '%q\n' "$file"
done

If the regex is too complex for translating it to extended globs then you can filter the files with a bash regex inside the for loop:

#!/bin/bash
```bash
#!/bin/bash

regex='([0-9]*\.) [0-9]*_to_([0-9]*\.) [0-9]*.sql'

for file in "$SCRIPT_PATH"/oaa_partition/upgrade/*.sql
do
    [[ $file =~ /$regex$ ]] || continue
    printf '%q\n' "$file"
done

BTW, as is, your regex could match a lot of unwanted things, for example: 0_to_..sql

CodePudding user response:

You cannot use regular expression in for loop. It only supports glob patterns and that is as robust as a regex.

You will have to use your regex in find command as:

find . -mindepth 1 -maxdepth 1 -regextype egrep -regex '.*/([0-9]*\.) [0-9]*_to_([0-9]*\.) [0-9]*\.sql

To loop these entries:

while IFS= read -rd '' file; do
   echo "$file"
done < <(find . -mindepth 1 -maxdepth 1 -regextype egrep -regex '.*/([0-9]*\.) [0-9]*_to_([0-9]*\.) [0-9]*\.sql)
  • Related