Sample Header Ad - 728x90

Keep matching pattern in shell parameter expansion

8 votes
3 answers
4460 views
I can remove a pattern in a bash variable using ${variable##pattern} (leading) or ${variable%%pattern} (trailing). But I can't find a bash-only way to keep the pattern and throw the rest. I know there are solutions using sed, awk, or grep, but I want to know if there is a reasonably efficient bash-only solution that I am overlooking? PS: This isn't just an idle question. The initial problem is that I want to process files where the name contains a pattern (technically, '[A-Z]+([A-Z])-[0-9][0-9]+([0-9])': capital letters followed by a dash and digits) and I want to use the same pattern to list the files and extract the matching string for further processing.
Asked by xenoid (9288 rep)
Jul 7, 2021, 12:48 PM
Last activity: Dec 25, 2024, 05:18 PM