LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-28-2008, 02:49 AM   #1
aristotled
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

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 !
 
Old 11-30-2008, 09:35 AM   #2
pixellany
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
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.)
 
Old 11-30-2008, 03:00 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 727

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

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


Reply


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 10:45 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 06:26 AM.

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