• Edit variable > new variable

    From Ben Everitt@21:1/5 to All on Thu Feb 24 14:45:20 2022
    All,

    I have three issues:
    1. I'm very new to code, so some aspects of regular expression notation are a challenge.
    2. Satisfying the bracketing necessary to edit a string (exec sed, or regsub) 3. Taking that edited output and writing it to a new variable.

    I'm taking the prompt on devices that's a hostname ending with a colon.
    IE: devicename:
    I'm grabbing it with expect_out(buffer). I need to pipe it to sed or regsub to remove the colon and create a new variable that I can reference for file naming.

    Any help would be much appreciated. I've exhausted my Expect book as well as googling the issue from any angle I can come up with.

    After trying all the iterations I can come up with, here's my current progress: set hostname [$expect_out(buffer) | exec sed {s/://}]

    -Ben

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Leitgeb@21:1/5 to Ben Everitt on Fri Feb 25 00:00:42 2022
    Ben Everitt <[email protected]> wrote:
    I'm taking the prompt on devices that's a hostname ending with a colon.
    IE: devicename:
    I'm grabbing it with expect_out(buffer).

    I need to pipe it to sed or regsub to remove the colon and create a new variable that I can reference for file naming.

    Just remove colons from the end of the string:
    set hostname [string trimright $expect_out(buffer) ":"]

    remove all colons - even any in the middle of the string:
    set hostname [string map {: ""} $expect_out(buffer)]

    do it with regular expressions:
    regsub -all -- {:} $expect_out(buffer) {} hostname
    regsub can write directly to a variable, therefore no "set ... [...]" here.

    Piping to "sed" is among the worst solutions to that task ;-)

    After trying all the iterations I can come up with, here's my current progress:
    set hostname [$expect_out(buffer) | exec sed {s/://}]

    Just for the sake of learning tcl, that would be:
    set hostname [exec sed {s/://} << $expect_out(buffer)]

    Or to really use a pipe, just to see how that would be syntactically done:
    set hostname [exec echo $expect_out(buffer) | sed {s/://}]
    But this has some very rough edges, and may lead to dangerous bugs,
    if the expect_out(buffer) ever contains certain "nasty" characters
    at the start of the string. Just don't do it that way!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to Ben Everitt on Thu Feb 24 19:01:32 2022
    On 2/24/22 5:45 PM, Ben Everitt wrote:

    Any help would be much appreciated. I've exhausted my Expect book as well as googling the issue from any angle I can come up with.

    After trying all the iterations I can come up with, here's my current progress:
    set hostname [$expect_out(buffer) | exec sed {s/://}]

    -Ben


    Hello,

    This is a basic string manipulation which Tcl excels at. Try this:


    set hostname [string trimright $expect_out(buffer) ":"]

    or

    set hostname [string range $expect_out(buffer) 0 end-1]

    or
    ...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jtyler@21:1/5 to Ben Everitt on Thu Feb 24 16:43:40 2022
    On 2/24/2022 2:45 PM, Ben Everitt wrote:

    Any help would be much appreciated. I've exhausted my Expect book as well as googling the issue from any angle I can come up with.


    Check out Ashok's great tcl language book, chock full of great examples
    you can cut and paste if you get the pdf version of the book.

    https://apnadkarni.gumroad.com/l/tclprog

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Everitt@21:1/5 to All on Fri Feb 25 14:44:05 2022
    Fantastic, thank you all. Looking for Ashok's book now. The extra TCL language that my Expect book doesn't cover will hopefully fill in my gaps.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)