Surabh S
Surabh S

Reputation: 11

how to trim the absolute directory path?

I have a absolute path to directory halo: /pkg/check/power/halo

I want to trim the absolute path to only: /pkg/halo

how can i do that using regex or pcreCompile function or unix?

Upvotes: 0

Views: 198

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137557

When working with paths, you're strongly recommended to use file split and file join as they handle weirdnesses you're not aware of.

set path /pkg/check/power/halo
set pieces [file split $path]
set result [file join {*}[lrange $pieces 0 1] [lindex $pieces end]]

Or (removing pieces rather than selecting them):

set result [file join {*}[lreplace $pieces 2 end-1]]

Upvotes: 3

Fravadona
Fravadona

Reputation: 16865

With tcl:

set path "/pkg/check/power/halo"
set path [ split $path / ]
set path /[lindex $path 1]/[lindex $path end]

Upvotes: 2

Related Questions