1

I'm trying to split the following string : Groups/Group#1.rpt to get only the word Group#1.rpt using tcl ; I did the following but the output was empty :

set verifyFile "Groups/Group#1.rpt" 
set verify_file_name [echo $verifyFile |cut -d "/" -f 2]
2
  • Please try to use pure Tcl when possible instead of doing shell commands. All the builtin commands are here: tcl.tk/man/tcl/TclCmd/contents.html The echo command is not builtin Tcl, but your particular Tcl interpreter allows it and other shell commands. Calling echo prints to stdout, it does not return a value. The pipe operator after echo is probably piping to a subshell that does the cut and then exits the subshell. Using just pure Tcl, like the split or file tail commands will do what you expect. Commented Oct 10, 2021 at 19:34
  • @ChrisHeithoff We prefer to use tcl-lang.org for URLs these days. Same content, same servers, just different DNS. Commented Oct 11, 2021 at 14:06

2 Answers 2

2

This is a job for a specific built-in: file tail!

set verify_file_name [file tail $verifyFile]

Don't use string manipulation directly for this; there are some complex nuances on different platforms. This command handles all the tricky edge cases for you.

Sign up to request clarification or add additional context in comments.

Comments

2

Donal's file tail answer is perfect for removing the directories from a file path.

If you have a "CSV" string using slash as the delimiter, you can use split and lindex

set verify_file_name [lindex [split $verifyFile /] end]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.