user389955
user389955

Reputation: 10467

Remove dash separated numbers from the end of string in Bash

XX-XX-XXXXX-ddd-dd-ddddd is composed of two parts:

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

Answers (1)

oguz ismail
oguz ismail

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

Related Questions