LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed in tcl (https://www.linuxquestions.org/questions/linux-newbie-8/sed-in-tcl-686794/)

aristotled 11-28-2008 01:49 AM

sed in tcl
 
I started learning sed just a month back and a newbie even in tcl.
I have been trying some scripting in tcl involving variables and unable to interpret some strange behaviour involving sed in tclsh.
Would appreciate if someone can explain why the following work/not work.

1) Double quotes vs single quotes in tclsh

1a) Works in tclsh (double-quotes works)
exec echo U7/unm_sub_system/tt_3282_1/jmp_0/jhb_addr0_pp_reg_4 | sed "s,unm,PuPnPm,g"

1b) Does not work in tclsh (single quotes gives error)
exec echo U7/unm_sub_system/tt_3282_1/jmp_0/jhb_addr0_pp_reg_4 | sed 's,unm,PuPnPm,g'

2)Trying to replace / with \/ in variable in tclsh.
set strtENC "U7/unm_sub_system/tt_3282_1/jmp_0/jhb_addr0_pp_reg_4"

2a) Does not work
exec echo $strtENC | sed "s,\/,\\\/,g"
Returns : U7/unm_sub_system/tt_3282_1/jmp_0/jhb_addr0_pp_reg_4

2b) Does not work
exec echo $strtENC | sed "s,/,\\\/,g"
Returns : U7/unm_sub_system/tt_3282_1/jmp_0/jhb_addr0_pp_reg_4

2c)Works
exec echo $strtENC | sed "s,/,\\\\/,g"
Returns : U7\/unm_sub_system\/tt_3282_1\/jmp_0\/jhb_addr0_pp_reg_4

2d) Works
exec echo $strtENC | sed "s,/,\\\\\/,g"
Returns : U7\/unm_sub_system\/tt_3282_1\/jmp_0\/jhb_addr0_pp_reg_4

Can someone please interpret why it works the way it does!

Thanks in advance !

pixellany 11-30-2008 08:35 AM

The "\" character---AKA "backslash" or "escape"---changes the meaning of the following character....
from special to literal
OR
from literal to special

....depending on context.

sed 's,/,\,' ##attempts to replace "/" with a literal ",", but fails because the closing "," is now missing

sed 's,/,\\,' ##attempts to replace "/" with a literal "\"---works correctly.

sed 's,/,\\\,' ##attempts to replace "/" with a literal "\,", but fails because the closing "," is now missing

Q#1: I have no clue. You have no special characters or variable calls in the "s" command, so the quoting should not matter. (Try it with NO quotes.)

makyo 11-30-2008 02:00 PM

Hi.

After some experimentation, I conclude that tcl exec function sets up its own command pipelines. Because it sets up the pipeline, it does not do single quote substitution in the way that the shell does (e.g. sh, bash, etc). For tcl, grouping is done with double quotes and curly braces, the quotes allowing substitution.

To get single quotes to a command that you want executed, you could do:
Code:

set x [ exec bash -c "echo abc | sed 's,b,x,'" ]
which produced for me:
Code:

axc
In this specific instance, braces could be used in place of the double quotes.

It seems like this could get very tedious if you need to do many of these execs, especially if you needed mixtures of quotes. Perhaps there are other ways to do what you wish to do with tcl statements.

I did not investigate the backslash situation, but it may make more sense once you get used to the idea that exec does not call the shell ... cheers, makyo

Ref: Practical Programming in Tcl and Tk, 3rd, Brent Welch


All times are GMT -5. The time now is 11:21 AM.