trim a source file path
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}//'
精彩评论