开发者

trim a source file path

开发者 https://www.devze.com 2023-02-03 03:46 出处:网络
I need t开发者_开发技巧o trim the following path in a unix shell script,please kindly suggest input- /vobs/java/server/forms/data/Branch/semanticexplorer.pls

I need t开发者_开发技巧o trim the following path in a unix shell script,please kindly suggest

  • input- /vobs/java/server/forms/data/Branch/semanticexplorer.pls
  • output- server/forms/data/Branch/semanticexplorer.pls


You've not given us any more general criteria by which to trim - so I'm chopping the fixed first two components.

The mechanism like this avoids executing a process:

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

Bash has some extensions that would be useful for more general path trimming. The Korn shell supports the ${var#prefix} notation.

You can also use:

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

This allows you to vary the prefix and still remove it.


In most shells, the brute force approach is:

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

In Bash:

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


echo $pathname | sed -E 's/\/([^/]*\/){2}//'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号