There are lots of solutions for this working on a single character, but not for substrings. I have a close solution, but it deletes everything after the last occurrence of a substring:

$ echo pkg-new-pkg-20180925-090719.x86_64 | sed -n -e "s/^.*pkg-//p"

What I want is:

$ echo pkg-new-pkg-20180925-090719.x86_64 | ???

The substring I'm splitting on is pkg- in this case. Any ideas?

  • You are mentioning "splitting" the string on a substring. Could you elaborate on this? Splitting pkg-new-pkg-20180925-090719.x86_64 on the string pkg- would result in the three strings "" (an empty string), new- and 20180925-090719.x86_64.
    – Kusalananda
    Sep 26, 2018 at 15:48

2 Answers 2


In a variable:

printf '%s\n' "${var#*pkg-}"

${var#pattern} removes the shortest leading part that matches the pattern (${var##pattern} for the longest part).

On a text input stream:

sed 's/pkg-/\

That replaces the first occurrence of pkg- with a newline character (a character which otherwise doesn't occur in the pattern space), and then removes everything up to that one newline character. Some sed implementations also allow you to write it sed 's/pkg-/\n/;s/.*\n//' though that's neither portable nor standard.

With ast-open sed (also works with ssed -R):

sed -E 's/.*?pkg-//'

With perl:

perl -pe 's/.*?pkg-//'

Where .*? is the non-greedy version of .*.

  • Awesome that does the trick, thanks a lot :) Sep 26, 2018 at 16:16

I have done by below mentioned 2 methods

First method

     echo "pkg-new-pkg-20180925-090719.x86_64"| sed "s/pkg-//"

Second method

echo "pkg-new-pkg-20180925-090719.x86_64"| awk -F "-" '{$1="";print $0}'| sed 's/^ //g'| sed "s/ /-/g"


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .