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...
...outputting the expected results:
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:
...results in this:
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).
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:
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:
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
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:
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:
...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...
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:
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
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)
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)
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)
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)
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)
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)
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)
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)