Martin G
Martin G

Reputation: 18109

Remove numbers and spaces from beginning of strings in array

I read in file entries into an array like this:

readarray files < $MY_DIR/file_list.cfg

file_list.cfg could look like this:

1  file1.cfg
2   some_other_file.cfg
3  yet_another_file_name

How do I remove any numbers and spaces before the actual file names in bash. I would like to modify the array after it has been read rather than filtering the content while populating the array.

Upvotes: 1

Views: 51

Answers (1)

gniourf_gniourf
gniourf_gniourf

Reputation: 46883

After populating your array (use the -t switch to remove trailing newline, and use lower case variable names):

mapfile -t files < "$my_dir/file_list.cfg"

you can use parameter expansions:

files_clean=( "${files[@]##+([[:digit:][:space:]])}" )

This assumes that you have the extglob shell option on. Use

shopt -s extglob

to turn it on.

Upvotes: 1

Related Questions