Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-28-2008, 01:49 AM   #1
LQ Newbie
Registered: Nov 2008
Posts: 1

Rep: Reputation: 0
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

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 !
Old 11-30-2008, 08:35 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
The "\" character---AKA "backslash" or "escape"---changes the meaning of the following character....
from special to literal
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.)
Old 11-30-2008, 02:00 PM   #3
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep: Reputation: 75

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:
set x [ exec bash -c "echo abc | sed 's,b,x,'" ]
which produced for me:
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

Last edited by makyo; 11-30-2008 at 02:01 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot find Tcl dll ( Perl's Tcl bridge ) Xyem Linux - Software 2 08-08-2006 09:45 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration