[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Regular expression replacement in SED.
On Wed, 5 Apr 2000, Robert Speed wrote:
> Quick question for any sed people;
>
> I'm trying to perform a regular expression replacement from sed
> but by passing params within a bash script.
> However, I'm a tad stuck at how to force the command line
> to stop interpreting white space literally.
>
> ie.
> if (from the cli) I ;
> sed 's/%NAME%/Fred Flintstone/' letterin > letterout
>
> I can replace all instances of %NAME% in letterin with
> Fred Flintstone and send the output to letterout.
>
> Fine, fair enough.
> The problem I have now is how to automate this from within a shell
> script.
>
> So, what I have in the script is the following;
>
> SedCommand=\'s\/%NAME%\/$Recipient\/\'
> echo sed $SedCommand $InputDoc \> $DocToSend
> sed $SedCommand $InputDoc > $DocToSend
>
> (Note, that they're not Vs in there, but escaped \ !)
Actually, they're not, they're escaped '/'s :)
> Now this is giving the output;
>
> sed 's/%NAME%/Robert Speed/' letter2 > form_rms.doc
> sed: 's/%NAME%/Robert is not a recognized function.
I've just done some tests and found that both GNU sed 2.05 and SunOS
5.7's sed will allow regexps with unescaped spaces in them (that's
not to say that bash won't need them escaped if you're not using
quotes.)
u9rah@suna33:~$ echo $PATH | sed "s/bin/spaced bin/g"
/home/u9rah/spaced bin:/usr/sspaced bin:/usr/local/share:/usr/dt/spaced
bin:/usr/openwin/spaced bin:/usr/local/spaced bin:/usr/spaced
bin:/usr/ucb:/opt/SUNWspro/spaced bin:/usr/ccs/spaced
bin:/usr/local/bcp/spaced bin:.
u9rah@suna33:~$ echo $PATH | gsed "s/bin/spaced bin/g"
/home/u9rah/spaced bin:/usr/sspaced bin:/usr/local/share:/usr/dt/spaced
bin:/usr/openwin/spaced bin:/usr/local/spaced bin:/usr/spaced
bin:/usr/ucb:/opt/SUNWspro/spaced bin:/usr/ccs/spaced
bin:/usr/local/bcp/spaced bin:.
I'm unable to replicate the error message that you got; don't know
what's up there. Your message would seem to indicate that the regexp is
unterminated, which usually gives "Unterminated `s' command" in GNU sed.
Anyway, the following shell script works fine for me:
#!/usr/local/bin/bash
Recipient="spaced bin"
echo $PATH | gsed s/bin/"$Recipient"/g
And produces:
u9rah@suna33:~$ bash test.sh
/home/u9rah/spaced bin:/usr/sspaced bin:/usr/local/share:/usr/dt/spaced
bin:/usr/openwin/spaced bin:/usr/local/spaced bin:/usr/spaced
bin:/usr/ucb:/opt/SUNWspro/spaced bin:/usr/ccs/spaced
bin:/usr/local/bcp/spaced bin:.
u9rah@suna33:~$ bash --version
GNU bash, version 2.03.0(3)-release (sparc-sun-solaris2.7)
Copyright 1998 Free Software Foundation, Inc.
However, changing "$Recipient" to '$Recipient' produces
u9rah@suna33:~$ bash test.sh
/home/u9rah/$Recipient:/usr/s$Recipient:/usr/local/share:/usr/dt/
$Recipient:/usr/openwin/$Recipient:/usr/local/$Recipient:
/usr/$Recipient:/usr/ucb:/opt/SUNWspro/$Recipient:
/usr/ccs/$Recipient:/usr/local/bcp/$Recipient:.
So it may be that you need to use `"`s instead of `'`s. Also, there's
no need to escape '/'s in bash; it's not a special character, just gets
used a lot for seperating directories :)
> The key point here is that the command line is interpreting the
> space in the variable $Recipient literally.
Actually, I believe the problem is too much escaping; you're actually
passing the `'` character as part of the arguement to sed, which is bad.
using `'`s tells bash to keep the contained string as a whole (and do
other things, but they're unimportant); in your setting of $SedCommand,
you've escaped it and told bash to actually make them a part of the
string, rather than use them as markers for the contained string.
I find that escaping can be a major pain in the behind; you've just got
to remember what you want, where you want it, and what's putting it
there :)
> I've tried 'escaping' the name in the source datafile (where the
> recipients name is coming from), but to no avail...
That's not suprising; like I said above, both of the seds that I've
tried allow unescaped spaces in regexps.
Hope I've helped,
Bob
--
Bob Ham: bob [at] timecity.org http://flux.mentaltempt.org/~node/
IRC: 'Bob' on irc.openprojects.net: #Slashdot #TimeCity
ICQ: 4396425 'The Tek' & 27699423 'The Tek.'
Time City Level Designer
Time City: http://www.timecity.org/
My work: http://flux.mentaltempt.org/~node/tc/
This email is Copyright (C) 2000 Robert Ham. Copyright is protected in law
in the UK and by treaty in other countries. Permission to reproduce is
strictly forbidden. If you wish to reproduce this email's content, please
apply by email to bob [at] timecity.org.
---------------------------------------------------------------------
Sheffield Linux User's Group - http://www.sheflug.co.uk
To unsubscribe from this list send mail to
- <sheflug-request [at] vuw.ac.nz> - with the word
"unsubscribe" in the body of the message.
GNU the choice of a complete generation.