Orlo
Orlo

Reputation: 828

Sed: replace a specific part

I need to replace a specific part of a link

for example:

http://sub.somedomain.com/s/be2b46b4cb28ed64fe59d191cb600117/2013/image.jpg

to

http://sub.somedomain.com/s/123123/2013/image.jpg

what I've tried:

echo "http://sub.somedomain.com/s/be2b46b4cb28ed64fe59d191cb600117/2013/image.jpg" | sed "s@/s/(.+?)/@123123@g"

Upvotes: 5

Views: 143

Answers (1)

ales_t
ales_t

Reputation: 2017

sed "s@/s/[^/]*/@/s/123123/@g"

Problems with your regex:

  • brackets ( and ) are regular characters in basic regular expressions (BRE) (and they are not really needed here)
  • + is also not special in BRE
  • if you enabled extended regexes with sed -E, you would match too much because you don't stop at the next slash

Upvotes: 5

Related Questions