sed escape character for comment string "/*"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed escape character for comment string "/*"
# 1  
Old 05-23-2011
sed escape character for comment string "/*"

Good afternoon all,
I'm hoping my newbie question can help bolster someone's street_cred.sh today.

I'm trying to "fingerprint" SQL on its way into the rdbms for a benchmarking process (so I can tie the resource allocation back to the process more precisely).

To do this, I'm essentially picking out the SQL into a variable, and trying to replace the first whitespace character after the first word with my fingerprint, in this case, a benchmark Id and a Run Id.

Oddly, the big hangup seems to be the relatively simple sed substitution... This works just fine...

Code:
bmid=001;runid=000001;taskbody="Select Date;";echo $taskbody | sed "s/ /&\/\* bm=$bmid run=$runid \*\/ /";

...outputting the expected results:
Code:
Select /* bm=001 run=000001 */ Date;

But when I try to use command substitution to save the results back to a variable (I have further processing to do), the substitution for the astrisk "\*" part of my sed command is being treated instead as an "ls \*" function.

Thus this:
Code:
bmid=001;runid=000001;taskbody="Select Date";taskbody=`echo $taskbody | sed "s/ /&\/\* bm=$bmid run=$runid \*\/ /"`; echo $taskbody

...results in this:
Code:
Select /bin /boot /data /dev /etc /home /lib /lib64 /lost+found /media /mnt /opt /proc /root /sbin /srv /sys /tmp /usr /var bm=001 run=000001 */ Date

I tried double (and triple, and quadruple) escape characters, to see if they'd persist outside of the backquotes, but it doesn't seem to make any difference.

I searched around the forums, but I didn't see anything threads about "/*" comment character escape problems. So either I'm the first person ever to do this (right...) or I'm missing something blindingly obvious (more likely).

Thoughts?

Thanks!
Stephen
# 2  
Old 05-23-2011
Your problem is that within double quotes, the shell will expand file globbing "wildcards" and you'll get a list of files and directories in the present directory for your "*". Usually this is solved by using single quotes, no expansion done within singles by the shell, but that won't allow you to use your variables as you'd like. I'm not a huge fan of this, but it does work if you "mix" your quotes:

Code:
sed 's/ /&\/\* bm='"$bmid"'run='"$runid"' \*\/ /'

Single quotes round everything except the variables; doubles round those. I don't like this because it's a maintenance headache. I also like to make a practice of not using escapes if I don't have to -- easier to read:

Code:
sed 's! !&/* bm='"$bmid"'run='"$runid"' */ !'

wil do the same thing. When using sed on file paths, or anything with slants, use an alternate delimiting character (!) in this case. It still uses the mixed quotes, but IMHO it's a bit easier to read.

Last edited by agama; 05-23-2011 at 07:43 PM.. Reason: clarification
# 3  
Old 05-23-2011
Hey agama ~

Your point on alternate delimiters is a great one - It does make the line much easier to read.

Unfortunately, mixing the quotes didn't solve the problem - this revised code containing your changes (and simplified further with hardcoded values) still interprets asterisk as list commands:

Code:
tb=`echo "Select Date" | sed 's! !&\/\* bm=001 run=000001 \*\/ !'`; echo $tb

It seems to be related to the backquote / command substitution...
If I copy-out the variable-set part of the line, the code returns the expected results - except those results are not saved back into the variable, they're simply echo'ed to the terminal.

Thus, this sub-string of the above code returns the correct value:
Code:
echo "Select Date" | sed 's! !&\/\* bm=001 run=000001 \*\/ !'

...except the variable $tb isn't updated. Wrap the tb=`[]` around it, and it misbehaves.

I might be confused on the way command substitution works... I thought it basically ran everything within the backquotes first, then stdout the results like any completed function. This is why I'm confused why the bottom code works fine, but the top one does not. Maybe someone needs to correct my thinking...

Smilie

Thanks!
# 4  
Old 05-23-2011
Your problem isn't where you think it is. Your backquoted command is working just as you expect it to, but when you echo $tb you are expanding the contents of $tb and then passing them to echo. Since the contents of the variable contains asterisks, they are expanded on the echo command line, not as a part of the sed pipeline. Try this:

Code:
tb=`echo "Select Date" | sed 's! !&\/\* bm=001 run=000001 \*\/ !'`; echo "$tb"

You should be able to step back and insert the variables.

---------- Post updated at 21:04 ---------- Previous update was at 20:58 ----------

And your perception of how back quoting works is just fine. If you are using Kshell or bash, $(command) is the preferred usage; back ticks are deprecated in Kshell (and I assume in bash, but I don't know that for fact).

Last edited by agama; 05-23-2011 at 10:02 PM.. Reason: embarrassing typo
# 5  
Old 05-24-2011
Aha - that's the ticket! Thanks for the help Agama, worked like a charm!

Also, thanks for the depreciation tip - I'll make a point of moving everything over to $(command) syntax.

Thanks!
Stephen
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed random \n for "n" range of character occurrences

I'd like to put paragraph breaks \n\n randomly between 5 - 10 occurrences of the dot character (.), for an entire text file. How to do that? In other words, anywhere between every 5 -10 sentences, a new paragraph will generate. There are no other uses of the (.) except for sentence breaks in... (11 Replies)
Discussion started by: p1ne
11 Replies

2. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

3. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

4. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

5. Shell Programming and Scripting

Script to search a string which is in between "" and replace it with another character

Hi, I am trying to search a string from a text file which is in between "" (Double Quotes) (Eg: "Unix"), and replace it with a | where ever it is appearing in the text file and save the file. Please help me. -kkmdv (6 Replies)
Discussion started by: kkmdv
6 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Solaris

Solaris escape my script from "-" to "/226"

Hello everyone. I beg your guys pardon please. I try to ls -al in many path/directories. So, I put the code in text file which look like below; ls -al / ls -al /etc ls -al /etc/default ... however, when I paste it to Solaris over SecureCRT, it seems the code was escaped from "-" to... (0 Replies)
Discussion started by: Smith
0 Replies

9. Shell Programming and Scripting

removing the "\" and "\n" character using sed or tr

Hi All, I'm trying to write a ksh script to parse a file. When the "\" character is encountered, it should be removed and the next line should be concatenated with the current line. For example... this is a test line #1\ should be concatenated with line #2\ and line number 3 when this... (3 Replies)
Discussion started by: newbie_coder
3 Replies

10. Shell Programming and Scripting

Sed - How to escape variable number of "/" (slash) ?

Hi, Can you tell me how to escape a variable number of slash characters in sed "/" ? In the script the code looks like this: cat $file_to_update | sed s/^$param/$param=$tab2*\#\*/1 And the $tab2 value is a path so it will have a number of "/" charracters. # cat db.cfg | sed... (4 Replies)
Discussion started by: majormark
4 Replies
Login or Register to Ask a Question