Reputation: 10467
XX-XX-XXXXX-ddd-dd-ddddd
is composed of two parts:
XX-XX-XXXXX
is composed of letters, it may contain one or multiple dashes, e.g. abc-de-fghij
, and abcde-fghi-j
.ddd-dd-ddddd
is composed of digits, again, it may contain one or multiple dashes, e.g. 123-456-7890
, 12-3-456-7890
.What is an easy way to get the XX-XX-XXXXX
in Bash? For example, suppose I have abc-de-fghij-123-456-7890
, how do I get abc-de-fghij
?
Upvotes: 0
Views: 456
Reputation: 50795
Using %%
variant of parameter expansions, you can remove everything including and following the first dash that is immediately followed by a digit.
$ str=abc-de-fghij-123-456-7890
$ echo "${str%%-[0-9]*}"
abc-de-fghij
Upvotes: 1