The "s///" operator will return true or false (1 or 0), and that is what the "print" function will get. Your output file will have a "0" or a "1" for each line of the input file, instead of the actual data.print NEW_FILE s/foo/bar/g;
Also, you're likely to run this script more than once (since you probably won't get everything right the first time), so you probably want the edited versions of files to be in a different directory (keep the source files and source directory unchanged), and you surely don't want to open the output files with ">>$filename".
Since perl gets used for this sort of thing a lot, there are a lot of short-cuts to make it easier. You could do the edit like this:
As for the FTP part, if the remote machine is running an anonymous-ftp server, and the files in question are available that way, then Net::FTP is fine. But if you need to use a particular (non-anonymous) user account and a password, you really should use Net::SFTP.#!/usr/bin/perl use strict; # suppose original files are in directory "source", and # we want to put edited versions into directory "edited": my @files = <source/*>; # that's called a "file glob" for my $ifile ( @files ) { my $ofile = $ifile; $ofile =~ s/source/edited/; open( I, "<", $ifile ); open( O, ">", $ofile ) or die "$ofile: $!"; while (<I>) { s/foo/bar/g; print O; } close I; close O; }
I would recommand that you keep the (S)FTP stuff in a separate script from the editing stuff. (It's actually more likely that you don't need a perl script at all to do the file transfers between machines, but if you want to make up a perl script to "reinvent" the existing (s)ftp programs, go ahead.)
The point is that you can write a re-usable file-transfer script that will be handy for lots of occasions (assuming the standard tools are less handy), and you can even write a re-usable editing script for making changes to a list of files, which will be handy for lots of occasions (it is possible to provide a list of regex substitutions as an input). Both of these things are relatively easy.
But if a script is going to do both file transfers and editing, that one script will be larger and more complicated and will take longer to write; and making it re-usable in any practical sense is going to be a lot more difficult.
In reply to Re^3: FTP and update files from a list
by graff
in thread FTP and update files from a list
by lev36
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |