2011-01-05 96 views
0

我需要修剪在UNIX shell脚本下面的路径,请您建议修剪源文件路径

  • 输入 - /vobs/java/server/forms/data/Branch/semanticexplorer.pls
  • 输出 - server/forms/data/Branch/semanticexplorer.pls
+0

是目录层次结构/的VOB/java的总是相同的或者它有什么不同? – roundrobin 2011-01-05 13:20:56

回答

3

你”我们没有给出任何修剪的一般标准 - 所以我砍掉了固定的前两个组件。

这样的机制避免了执行过程:

input=/vobs/java/server/forms/data/Branch/semanticexplorer.pls 
output=${input#/vobs/java/} 

Bash有一些扩展,这将是更普遍的路径微调有用。 Korn外壳支持${var#prefix}表示法。

您还可以使用:

prefix=/vobs/java/ 
input=/vobs/java/server/forms/data/Branch/semanticexplorer.pls 
output=${input#$prefix} 

这可以让你改变前缀,仍然将其删除。


在大多数炮弹,蛮力的方法是:

input=/vobs/java/server/forms/data/Branch/semanticexplorer.pls 
output=$(echo $input | sed "s%/vobs/java/%%") 

在bash:

input=/vobs/java/server/forms/data/Branch/semanticexplorer.pls 
output=$(sed "s%/vobs/java/%%" <<< $input) 
0
echo $pathname | sed -E 's/\/([^/]*\/){2}//'