Re: sed replace with html code and variables



bourguignon@xxxxxxxxx wrote:

First off, I should mention that I am a sed neophyte. What little I
know I've cobbled together over the years, and each new use of it
feels like learning it over from scratch. That being said, I love the
power of it.

So, I'm trying to write a bash script that will do a replace among
several files; I've got the rest of the script down, so I simplified
it for this question (nevermind that the html doesn't work). When I
run this script, it either errors out with an "unterminated
substitution" error, or it just does nothing... Here's some code:

#!/bin/bash
NUM=4
ITEM=A001
sed s#'\<\!-- Thumb$NUM --\>'#' \<li\>\<a href="\.\.\/\.\.\/images\/
aprons\/$ITEM_$NUM_lg\.jpg" target="_blank" class="thumb"\>'#g
A001.html > new_A001.html


the line "<!-- Thumb1 -->" (Thumb2,3,4, etc) is in each template file
that I am running this command on...

It seems you are escaping too much here. "<" and ">" do not need to be
escaped; actually, escaping them might turn on special meaning with certain
versions of sed (eg, with GNU sed \< and \> mean beginning and end of word).
See these examples with GNU sed:

$ echo '<!-- Thumb1 -->' | sed -n '/<!-- Thumb[[:digit:]] -->/p'
<!-- Thumb1 -->
$ echo '<!-- Thumb1 -->' | sed -n '/\<!-- Thumb[[:digit:]] --\>/p'
$

Here's how to use \< and \>, in case you're interested (GNU sed):

$ printf 'aa\nbaab\ncaa\naad\naa\n' | sed -n '/\<aa\>/p'
aa
aa

ie, match "aa" only when it's a word on its own.

Even more so, in the RHS of a substitution, you don't need to escape
anything (usually only \ - if not used for backreferences - and &). In your
case, since you want the shell to expand the values of the variables, you
have to put the sed script in double quotes, and thus you need to escape the
double quotes in the RHS. Bash has another caveat: the ! is interpolated
when in double quotes (but only on the command line), so you need to escape
that as well if you try the command interactively. In a script, ! can appear
unescaped in double quotes.

do I need to wrap the sed command in ticks or backticks? are my ticks
inside the #'s messing me up?? I've even escaped several characters
that I shouldn't have to out of frustration...

See above. This should work (I can't test it since you provided no sample
input):

#!/bin/bash
NUM=4
ITEM=A001
sed "s#<!-- Thumb${NUM} -->#<li><a
href=\"../../images/aprons/${ITEM}_${NUM}_lg.jpg\" target=\"_blank\"
class=\"thumb\">#g" A001.html > new_A001.html

(the sed part all on a single line)

If you use that from the command line, then escape the "!" as well.

--
echo 0|sed 's909=oO#3u)o19;s0#0ooo)].O0;s()(0bu}=(;s#}#.1m"?0^2{#;
s)")9v2@3%"9$);so%op]t(p$e#!o;sz(z^+.z;su+ur!z"au;sxzxd?_{h)cx;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb;s/#.*//;s/.\(.\)/\1/g'
.



Relevant Pages

  • Re: Accessing full command line or parameters WITH QUOTES
    ... The problem is that VBScript doesn't allow access to the full command line ... rebuild the parameters with the quotes inserted properly. ... When I was writing the script on XP, I managed to cheat and grab the ...
    (microsoft.public.scripting.vbscript)
  • Re: Accessing full command line or parameters WITH QUOTES
    ... The problem is that VBScript doesn't allow access to the full command line ... rebuild the parameters with the quotes inserted properly. ... as one argument to your script, ... following will run the php script (assuming php is ...
    (microsoft.public.scripting.vbscript)
  • Re: Accessing full command line or parameters WITH QUOTES
    ... rebuild the parameters with the quotes inserted properly. ... command line arguments. ... as one argument to your script, ... It follows that the command processor preserves at least ...
    (microsoft.public.scripting.vbscript)
  • Re: Launch chm file on WS within HTML
    ... escape another. ... However, in Java Script, the rules are ... The command line cd ... |> | and only surrounded the file path with two double quotes. ...
    (microsoft.public.scripting.wsh)
  • Re: Accessing full command line or parameters WITH QUOTES
    ... The problem is that VBScript doesn't allow access to the full command line ... rebuild the parameters with the quotes inserted properly. ... what is that escape? ... as one argument to your script, ...
    (microsoft.public.scripting.vbscript)