Using PERL, I'm trying to login to an FTP server, download all files from a specific directory and then move all these files to another directory on the server.
This is my code so far:
open( my $LFTP,'|-', "lftp -u $ftpuser,$ftppwd -e open $ftpserver" ) or die "Cannot open lftp: $!";
print $LFTP <<"END";
mirror $remoteFiles $datadir
renlist $remoteDir | "sed 's#\(.*\)#mv \"\1\" \"outgoing/archive\/\"#'" > list && source list && !rm list
END
close($LFTP) or die; # die unless lftp exit code is 0
The files download correctly but they won't move. I believe the problem is with the sed command.
The temp file created called "list" looks like that:
dir1/di2/file1.csv
dir1/di2/file2.csv
dir1/di2/file3.csv
dir1/di2/file4.csv
While it should be looking like that:
mv "dir1/di2/file1.csv" "outgoing/archive/"
mv "dir1/di2/file2.csv" "outgoing/archive/"
mv "dir1/di2/file3.csv" "outgoing/archive/"
mv "dir1/di2/file4.csv" "outgoing/archive/"
It seems like the sed regex is not working correctly. However, if I type the exact same command directly on the LFTP terminal, it works fine.
Could you please tell me what is wrong with my regex and why it won't work when executed from within a perl script?
Thank you in advance for your help....
Escaping of the \
character should do the trick:
print $LFTP <<"END";
mirror $remoteFiles $datadir
renlist $remoteDir | "sed 's#\\(.*\\)#mv \"\\1\" \"outgoing/archive\/\"#'"
Escaping the \
admits for perl's string literal syntax, resulting in the backslashes that escape sed's special chars.