简体   繁体   English

使用sed匹配两个字符串

[英]Using sed to match between two strings

example string: /Volumes/Macintosh HD/Users/test/ 示例字符串: /Volumes/Macintosh HD/Users/test/

I need the output to be the volume name, Macintosh HD 我需要输出为音量名称, Macintosh HD

I've tried the following: 我尝试过以下方法:

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

but the output is: testVolume/test 但输出是: testVolume/test

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM