user1630543
user1630543

Reputation: 67

Using sed to match between two strings

example string: /Volumes/Macintosh HD/Users/test/

I need the output to be the volume name, Macintosh HD

I've tried the following:

echo "/Volumes/testVolume/test/" | sed 's/\/Volumes\/\(.*\)\//\1/g'

but the output is: testVolume/test

Upvotes: 1

Views: 438

Answers (1)

Tuxdude
Tuxdude

Reputation: 49543

This should work:

sed 's#/Volumes/\([^/]*\)/.*#\1#'

Instead of using / to mark the search and replace expressions, you could use any other character as well. # is a commonly used character if your search or replace expressions contain the character / in them.

$ sed 's#/Volumes/\([^/]*\)/.*#\1#' <<< "/Volumes/Macintosh HD/Users/test/"
Macintosh HD

Upvotes: 3

Related Questions