On Tue, 2002-12-03 at 15:54, Shaw, Marco wrote:
> >From a script: main.sh, I need to:
> 1. Copy file ./x to /tmp/x
> 2. Read a variable from stdin, for example: read VALUE
> 3. Search through file /tmp/x for the keyword REPLACE, and replace it with ${VALUE}
> 
> I can't seem to figure out what combination of sed, perl, whatever I can use.
> 
> Marco
> 
> main.sh:
> #!/bin/sh
> 
> cp ./x /tmp/x
> read VALUE
> 
> # what I really want, but doesn't work:
> sed 's/REPLACE/${VALUE}/g' /tmp/x > /tmp/x.new
> 
> 
> 

The perl approach might help.

#!/usr/bin/perl 

# The static variables
$in_file="/tmp/x";
$out_file="/tmp/x.new";

use File::Copy;
copy("./x", "/tmp/x");

@ARGV == 1 or die "usage: scriptname REPLACEVALUE\n";

# Set the variables

($REPLACEVALUE) = @ARGV;

open(INHANDLE, $in_file ) || die "Can't open $in_file\n";
open (OUTHANDLE, ">> $out_file") || die "Sorry I can't add information to $out_file\n";

        while (<INHANDLE>) {
                        @lines = $_;
                        foreach $line(@lines)
                        {
                        #Replace crucial strings
                                chomp $line;
                                $line =~ s/REPLACE/$REPLACEVALUE/g;
                                print OUTHANDLE "$line\n";
                                }
                        }




-- 
Johnathan Bailes        BAE Systems ESI

 "UNIX was not designed to stop you from doing stupid things, because
 that would also stop you from doing clever things." - Doug Gwyn 
--- 



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to