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

  • bash, word splitting, and command substitution
    ... I would like to use the contents of a file as the command line ... arguments to a program, within a bash script. ... the file to try to control the word splitting ... single and double quotes. ...
    (comp.os.linux.development.apps)
  • 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: Launch chm file on WS within HTML
    ... > quotes), in Java Script need to be escaped? ... I just forget to escape the backslashes in the path. ... I finally found and downloaded Microsoft's Windows Script Technologies ...
    (microsoft.public.scripting.wsh)
  • Re: Problem with quotes
    ... I called a .bat file with the command in it ... > Double the quotes within the string as in ... ... When I try to put it into a script the required quotes breaks it. ...
    (microsoft.public.scripting.wsh)
  • Re: for the pros: how to run this command in crontab
    ... > i know i have to put some escape sign in here. ... I wouldn't try to put that whole command into the ... crontab entry if I were you. ... and have cron run the script. ...
    (comp.unix.questions)