[nycbug-talk] sed Question

Gordon Smith g at bin-arts.com
Thu Mar 30 18:40:40 EST 2006


Instead of attempting to put the shell variable in the midst of the sed
commandline,  you may want to build the complete commandline string within
another variable.  Concatenate the value of $fastest onto the string
representing the complete commandline.  You could then run the commandline
using either eval or exec.  Although it looks like you might want to use
eval, check out the man pages for each to see which is more appropriate to
your case.

For example, in my environment, the env var $VISUAL is set to "vi".  From
the shell prompt, If I wanted to edit the file "newfile" the hard way, I
might execute these commands:

$ commandline="$VISUAL newfile"
$ echo $commandline
vi newfile
$ eval $commandline
newfile: new file: line 1
(vi is displayed in all its spartan glory...)

Hope this helps - please let me know how it works for you.


-----Original Message-----
From: talk-bounces at lists.nycbug.org [mailto:talk-bounces at lists.nycbug.org]
On Behalf Of Kevin Reiter
Sent: Thursday, March 30, 2006 5:53 PM
Subject: [nycbug-talk] sed Question


I know there are a few scripting gurus on this list who might be able to 
help me out with this..

I'm trying to write a script that does the following:

1. Runs 'fastest_cvsup -c us' to find the fastest cvsup server
2. Assigns the value of "server.txt" (which is the fastest server) to a 
variable and then searches my "ports-sup" file for the existing server, 
and replaces it with the new one.

Sofar, everything works fine until I get to the replacement part of the 

Here's the script:


echo ""
echo "Finding the fastest cvsup server."
echo ""
fastest_cvsup -c us | grep 1st | cut -d : -f 2 > server.txt
sed 's/^[ \t]*//' server.txt > fast.txt
fastest=`cat fast.txt`
echo ""
echo "The fastest server seems to be: $fastest"
echo ""
echo ""
echo "Here we actually modify the file using sed."
echo ""
sed '2s/cvsup*.FreeBSD.org/$fastest/' ports-sup
echo ""
echo Done.
rm server.txt
rm fast.txt

I've tried replacing '$fastest' with 'BLAH' and I'm still doing 
something wrong, but I don't know what.  I've been hitting Google pretty 
hard for a few days looking for some examples/pointers (which has gotten 
me this far) to no avail.

I'm looking to replace:

*default host=cvsup11.FreeBSD.org


*default host=cvsup##.FreeBSD.org (where ## is the actual number, like 2 
or 14 or whatever.)

Any ideas, or pointers to let me know what I'm doing wrong?

% NYC*BUG talk mailing list
%Be sure to check out our Jobs and NYCBUG-announce lists
%We meet the first Wednesday of the month

More information about the talk mailing list