1

I have a file with extensions:

f1=f1.e1.e2.e3.sh.pl.cpp

I have an array containing a list of extensions:

exts=(sh pl py)

I want to remove the extensions in exts from the filename's. Is there a trick to do it, I'm thinking of something of this kind:

${f1%.${exts[@]}}

It obviously does not work.

0

I would construct a pattern from your exts array and then use sed :

f1=f1.e1.e2.e3.sh.pl.cpp
exts=(sh pl py)


function array_to_regex_alts() { # joins the array elements by \|
    echo -n "$1"; shift; printf "\|%s" "$@"
}

pattern="\.\($(array_to_regex_alts ${exts[@]} )\)"

echo $f1 | sed "s/$pattern//g"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.