devtech123
devtech123

Reputation: 15

trim a source file path

I need to trim the following path in a unix shell script,please kindly suggest

Upvotes: 0

Views: 341

Answers (2)

Jonathan Leffler
Jonathan Leffler

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

Michael Lowman
Michael Lowman

Reputation: 3068

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

Upvotes: 0

Related Questions