Reputation: 15
I need to trim the following path in a unix shell script,please kindly suggest
/vobs/java/server/forms/data/Branch/semanticexplorer.pls
server/forms/data/Branch/semanticexplorer.pls
Upvotes: 0
Views: 341
Reputation: 753525
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)
Upvotes: 3