Designer
Designer

Reputation: 11

how to remove characters in a string till the first occurrence of a string,

For ex=> set a 123/345/567/2345 , now i want to remove 123 and keep rest

expected output is 345/567/2345

How to do this in tcl without the use of awk '{print $1}'

set string 123/456/789
set idx [string last "/" $string]
set wanted [string range $string $idx-3 end]

Saw this approach in one of the posts but i want to make it so that 12345/43444/567676 should give me output as 43444/567676

Upvotes: 1

Views: 169

Answers (3)

Colin Macleod
Colin Macleod

Reputation: 4382

Here's another way using list operations:

(Tcl) 1 % set string 123/456/789
123/456/789
(Tcl) 2 % set list [split $string / ]
123 456 789
(Tcl) 3 % set l2 [lassign $list dummy]
456 789
(Tcl) 4 % set wanted [join $l2 / ]
456/789

Or the same thing as a one-liner:

set wanted [join [lassign [split $string / ] dummy] / ]

Upvotes: 0

sharvian
sharvian

Reputation: 654

How about this:

set input "123/345/567/2345"
regexp {^[^/]*/(.*)$} $input matched output
puts $output  ;# 345/567/2345

Upvotes: 0

TrojanName
TrojanName

Reputation: 5375

I think you need to use string first, not string last. And add 1 to idx to ignore the leading forward slash.

set string 12345/43444/567676
set idx [string first "/" $string]
set wanted [string range $string $idx+1 end]

Outputs:

43444/567676

Upvotes: 2

Related Questions