Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replace string in file

Reply
Thread Tools

replace string in file

 
 
wana
Guest
Posts: n/a
 
      10-01-2004
I want to replace a key word in a file with a string. Here is a
segment of code cut out of a script I wrote to help me generate
desktop application links.

<code>
open TEMPLATE, './template.desktop' or
die("could not open template.desktop file\n");
my @template = <TEMPLATE>;
foreach (@template)
{
s/EXEC/$ARGV[0]/g;
s/NAME/$ARGV[1]/g;
}
open OUTFILE, ">$ENV{'HOME'}/Desktop/$ARGV[2]" or
die("could not open output file\n");
print OUTFILE @template;
close OUTFILE;
</code>

I was just wondering if there was a better way.


Thank you to all for help on this question and all previous questions
I have submitted to this group for which all responses and
particularly certain special responses have been most helpful.

wana
 
Reply With Quote
 
 
 
 
Robert Sedlacek
Guest
Posts: n/a
 
      10-01-2004
wana wrote:

> I was just wondering if there was a better way.


Just an idea: You could use STDIN and STDOUT instead of opening
filehandles. I don't know if this is a good idea, that belongs to
your scenario. You could cut it down to (untested):

#!/usr/bin/perl
use warnings;
use strict;
use diagnostics;

while(<>) {
s/EXEC/$ARGV[0]/g;
s/NAME/$ARGV[1]/g;
print;
}

If you want to use a default file (eg. ./template.desktop) you could
use this one (again untested):

[...she-bang and use's...]

open TPL, '<./template.desktop'
or die('Unable to open template file.'."\n");
while(<TPL>) {
s/EXEC/$ARGV[0]/g;
s/NAME/$ARGV[1]/g;
print;
}
close TPL;

You can use this one by running

$ $SCRIPT exec_replace name_replace > file

The pro I see here is that you're not doing any buffering of the complete
content of the input file.

hth,
Robert

--
http://www.dunkelheit.at/

»Better to reign in hell than to serve in heaven«
-- John Milton, »Paradise Lost«

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-01-2004
"wana" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I want to replace a key word in a file with a string. Here is a
> segment of code cut out of a script I wrote to help me generate
> desktop application links.
>
> <code>
> open TEMPLATE, './template.desktop' or
> die("could not open template.desktop file\n");
> my @template = <TEMPLATE>;
> foreach (@template)
> {
> s/EXEC/$ARGV[0]/g;
> s/NAME/$ARGV[1]/g;
> }
> open OUTFILE, ">$ENV{'HOME'}/Desktop/$ARGV[2]" or
> die("could not open output file\n");
> print OUTFILE @template;
> close OUTFILE;
> </code>
>
> I was just wondering if there was a better way.
>


This is such a common use for perl that there's a couple command line
arguments available to make this a one-liner:

perl -pe"s/EXEC/arg0/; s/NAME/arg1/;" ./template.desktop >
$HOME/Desktop/arg2

Read about -p and -e in
perldoc perlrun

You may also be interested in -i, also in the above perldoc.

Paul Lalli


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-01-2004
wana wrote:
> I want to replace a key word in a file with a string. Here is a
> segment of code cut out of a script I wrote to help me generate
> desktop application links.
>
> <code>
> open TEMPLATE, './template.desktop' or
> die("could not open template.desktop file\n");
> my @template = <TEMPLATE>;
> foreach (@template)


There is on point in reading the whole file into an array when afterwards
you are looping through each line individually anyway. Better process each
line as you read it:

while (<TEMPLATE>) {.....

jue


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      10-02-2004
Paul Lalli wrote:

> This is such a common use for perl that there's a couple command line
> arguments available to make this a one-liner:
>
> perl -pe"s/EXEC/arg0/; s/NAME/arg1/;" ./template.desktop >$HOME/Desktop/arg2


The /g modifier is still required; "EXEC" could appear multiple times on one
line as could "NAME".
-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
Replace a string with another string in an XML file in VB.NET Amritha.Datta@gmail.com ASP .Net 1 09-17-2007 03:04 PM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
how to replace a string by other string in a text file? Umesh C Programming 17 06-21-2007 04:40 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments