replace string in file

W

wana

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
 
R

Robert Sedlacek

wana said:
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
 
P

Paul Lalli

wana said:
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
 
J

Jürgen Exner

wana said:
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
 
J

Joe Smith

Paul said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top