Search code examples
regexbashgrepglob

Method to grep multiple folders via a regex search term


I currently have a bash script to grep multiple folders of .txt files. The folder structure looks like this:

20200801
20200802
20200803
20200804
20200805
20200806
20200807
20200808
20200809
20200810
20200811
20200812
20200813
20200814
20200815
20200816
20200817
20200818
20200819
20200820
20200821
20200822
20200823
20200824
20200825
20200826
20200827
20200828

I have

read -p "Enter date: " Date

and

grep -r -h -P "$SearchKey" /mnt/f/Files/August2020/$Date

for a date input where i can input the date as

2020080[1-9]

to grep all folders between 1st and 9thof the month. However if I attempt

202008(0[1-9]|1[0-9])

the extended regex doesn't seem to work. I am looking for a way to be able to search within any dates I want.


Solution

  • You need extglob shell option and @ operator:

    shopt -s extglob 
    echo 202008@(0[1-9]|1[0-9])