How to search for multiple words of a specific pattern and separator?

Viewed 58

I'm trying to trim out multiple hex words from my string. I'm searching for exactly 3 words, separated by exactly 1 dash each time.

i.e. for this input:

wonder-indexing-service-0.20.0-1605296913-49b045f-19794354.jar

I'd like to get this output:

wonder-indexing-service-0.20.0.jar

I was able to remove the hex words by repeating the pattern. How can I simplify it? Also, I wasn't able to change * to +, to avoid allowing empty words. Any idea how to do that?

What I've got so far:

# Good, but how can I simplify?
% echo 'wonder-indexing-service-0.20.0-1605296913-49b045f-19794354.jar' | sed 's/\-[a-fA-F0-9]*\-[a-fA-F0-9]*\-[a-fA-F0-9]*//g'
druid-indexing-service-0.20.0.jar
# Bad, I'm allowing empty words
% echo 'wonder-indexing-service-0.20.0-1605296913-49b045f-.jar' | sed 's/\-[a-fA-F0-9]*\-[a-fA-F0-9]*\-[a-fA-F0-9]*//g'
druid-indexing-service-0.20.0.jar

Thank you!

EDIT: I had a typo in original output, thank you anubhava for pointing out.

3 Answers

You may use this sed:

s='wonder-indexing-service-0.20.0-1605296913-49b045f-19794354.jar'

sed -E 's/(-[a-fA-F0-9]{3,})+//' <<< "$s"

wonder-indexing-service-0.20.0.jar

Breakup:

  • (: Start a group
    • -: Match a hyphen
    • [a-fA-F0-9]{3,}: Match 3 or more hex characters
  • )+: End the group. Repeat this group 1+ times

If you want to use the + you have to escape it \+, but you can repeat matching 3 words prepended by a hyphen using a quantifier which also need escaping

\(-[a-fA-F0-9]\+\)\{3\}

Example

echo 'wonder-indexing-service-0.20.0-1605296913-49b045f-19794354.jar' | sed 's/\(-[a-fA-F0-9]\+\)\{3\}//g'

Output

wonder-indexing-service-0.20.0.jar

If you don't want to allow a trailing - then you can match the .jar and put that back in the replacement.

echo 'wonder-indexing-service-0.20.0-1605296913-49b045f-19794354.jar' | sed 's/\(-[a-fA-F0-9]\+\)\{3\}\(\.jar$\)/\2/g'
printf "wonder-indexing-service-0.20.0-1605296913-49b045f-19794354.jar" | cut -d'-' -f1-4 | sed s'@$@.jar@'
Related