mynameisJEFF
mynameisJEFF

Reputation: 4239

Bash: Extract filenames by pattern and insert them into an array

I have a list of files within a folder and I want to extract the filenames with the following pattern and insert them into array.

The pattern is that the file name always begin with either "MCABC_" or "MCBBC_" and then a date and then ends with ".csv"

An example would be "MCABC_20110101.csv" , ""MCBBC_20110304.csv"

Right now, I can only come up with the following solution which works but it is not ideal .

ls | grep -E "MCABC_[ A-Za-z0-9]*|MC221_[ A-Za-z0-9]*"

I read that it is bad to use ls. And I should use glob.

I am completely new to bash scripting. How could I extract the filenames with the patterns above and insert it into an array ? Thanks.

Update: Thanks for the answers. Really appreciate your answers. I have the following code

#!/bin/bash
shopt -s nullglob
files=(MC[1-2]21_All_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].csv)
echo ${#files[*]}
echo ${files[0]}

And this is the result that I got when I ran bash testing.sh.

: invalid shell option namesh: line 2: shopt: nullglob 1 (MC[1-2]21_All_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].csv)

However, if I just ran on the command line files=(MC[1-2]21_All_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].csv) and then echo ${files[*]}, I manage to get the output:

MC121_All_20180301.csv MC121_All_20180302.csv MC121_All_20180305.csv MC221_All_20180301.csv MC221_All_20180302.csv MC221_All_20180305.csv

I am very confused. Why is this happening ? (Pls note that I running this on ubuntu within window 10.)

Upvotes: 5

Views: 1716

Answers (2)

Matias Barrios
Matias Barrios

Reputation: 5056

This will work in BASH.

#!/bin/bash
for file_name in M*
do

    line="$line $( printf "${file_name%_*}")"
done
array=( $line )
echo "${array[2]}"

Another way :

#!/bin/bash

declare -a files_array
i=0
for file_name in M*
do
    files_array[$i]="$( printf "${file_name%_*}")"
    (( i++ ))
done

echo "${files_array[2]}"

Regards!

Upvotes: -1

Tom Fenech
Tom Fenech

Reputation: 74665

I think you can just populate the array directly using a glob:

files=( MC[AB]BC_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].csv )

The "date" part can certainly be improved, since it matches completely invalid dates like 98765432, but maybe that's not a problem.

Upvotes: 3

Related Questions