Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text modification

Status
Not open for further replies.

yahve

Programmer
Nov 14, 2000
159
CA
Hi,

I have a script that replaces the french accented caracters
by their html entities. I works great. I would like to improve it. I would like it to take the file name from the command line, back-up that file to whatever.bak and then modify the file and write it out to the original name.

How would I do that. How would I make my script back-up the file and then rewrite it with the modified text?

Thanks
 
this is what i would do:

$file = shift || die "no input file\n";
$filebak = $file . ".bak";

open(FILE, "$file") || die;
@file = <FILE>;
close(FILE);

open(BACK, &quot;>$filebak&quot;) || die;
foreach(@file)
{
print BACK $_;
}
close(BACK);

open(FILE, &quot;$file&quot;) || die;
foreach(@file)
{
tr/stuff/otherstuff/g;
print FILE $_;
}
close(FILE);


or you could just use system() or `` to copy the file to filename.bak before you modify it.
 
Thanks Luciddream,

I appreciate your input but I went a different route:

#!/usr/bin/perl -w
$infile = shift || die;
$outfile = $infile . &quot;.NEW&quot;;
open(IN,$infile) || die;
open(OUT,&quot;>$outfile&quot;) || die;
while(<IN>) {
tr/old/new/;
print OUT $_;
}
close(IN);
close(OUT);


I think it is simpler. But maybe you can help me out an other way. Can you tell me, simply, what is the difference between s/// and tr/// ?

Thank you

 
s/// does replacement. tr/// does translation..

with tr you can translate a range of characters to another range for example:

tr/[a-d]/[e-h]/g

translates every a to an e, every b to an f, every c to a g, and every d to an h.

with s you can only replace like this:

s/joe/dave/g

replaces every instance of joe with dave.
 
Ok Luciddream, I think I got that (maybe... english not being my 'forte'...).

If I can pick your brain some more:

1. how could I tell my script to process every file with a given extension (say .htm) in a directory?

I would like to give a path as the argument and have the script process each .htm file in that directory. Could I specify more than one extension? The first argument would be the directory and the second argument a LIST of extensions.

2. If in the script I change & to $#38; (s/&/$#38;/g) and also change é to $#233; (s/é/$#233;/g) how can I prevent the first command to change the & in the output of the second command? (I used $ because you'd see s/&/&/g if I did not)

Thanks


Vieux motard que jamais...
 
#!/usr/bin/perl

$dir = shift;
$exts = shift;

@ext = split(',', $exts);

chdir &quot;$dir&quot;;
@files = glob('*');

foreach $file (@files)
{
foreach $ext (@ext)
{
($filename, $fileext) = split('.', $file);
if($fileext eq $ext)
{
push(@good_files, $file);
}
}
}

#@good_files now holds all of the files you want to modify.

foreach $file (@good_files)
{
open(FILE, &quot;$file&quot;) || die;
@lines = <FILE>;
close(FILE);
foreach(@lines)
{
s/\&/\$\#38;/g;
s/é/\$\#233;/g;
}
open(FILE, &quot;>$file) || die;
print FILE @lines;
close(FILE);
}


i'm not sure what you mean in the regex's but, that's how i would do it. there's also a way to open directories like filehandles. i don't recall off the top of my head how to do this but i'm sure it isn't hard to find out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top