0

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.
  • And 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?

0

1 Answer 1

1

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
1
  • 1
    @ oguzismail: it works. Thank you very much!
    – user389955
    Jan 30, 2021 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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