Friday, June 10, 2011

Perl and Linux

任务:将名为dir的文件夹中所有java文件中的文本“Linux”替换成“Linuxidc”:
(1).sed方案:
sed -i "s/Linux/Linuxidc/g" `grep Linux -rl /home/dir`
(2).perl方案:
perl -p -i -e "s/Linux/Linuxidc/g" *.java

本篇文章来源于 Linux公社网站(http://www.linuxidc.com/)  原文链接:http://www.linuxidc.com/Linux/2008-02/10954.htm

File::Copy

use File::Copy;
 copy("file1","file2") or die "Copy failed: $!";
 copy("Copy.pm",\*STDOUT);
 move("/dev1/fileA","/dev2/fileB");

The copy function takes two parameters: a file to copy from and a file to copy to.

The move function also takes two parameters: the current name and the intended name of the file to be moved. If the destination already exists and is a directory, and the source is not a directory, then the source file will be renamed into the directory specified by the destination.

If possible, move() will simply rename the file. Otherwise, it copies the file to the new location and deletes the original.
sources: http://perldoc.perl.org/File/Copy.html

No comments:

Post a Comment