×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

text modification

text modification

text modification

(OP)
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

RE: text modification

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, ">$filebak") || die;
foreach(@file)
{
    print BACK $_;
}
close(BACK);

open(FILE, "$file") || 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.

RE: text modification

(OP)
Thanks Luciddream,

I appreciate your input but I went a different route:

#!/usr/bin/perl -w
$infile = shift || die;
$outfile = $infile . ".NEW";
open(IN,$infile) || die;
open(OUT,">$outfile") || 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

RE: text modification

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.

RE: text modification

(OP)
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...

RE: text modification

#!/usr/bin/perl

$dir = shift;
$exts = shift;

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

chdir "$dir";
@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, "$file") || die;
    @lines = <FILE>;
    close(FILE);
    foreach(@lines)
    {
        s/\&/\$\#38;/g;
        s/é/\$\#233;/g;
    }
    open(FILE, ">$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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close