免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1926 | 回复: 1
打印 上一主题 下一主题

file::find例子&&求助 [复制链接]

论坛徽章:
3
CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-03-13 15:38:15CU大牛徽章
日期:2013-03-13 15:38:52
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-08-30 22:26 |只看该作者 |倒序浏览
  1. use File::Find;

  2. @ARGV=('.') unless @ARGV;
  3. $dir=shift @ARGV;
  4. find(\&edits, $dir);
  5. sub edits() {
  6.              $seen=0;
  7.              if( -f and /.htm?/ )
  8.                 { $file=$_;
  9.                    open FILE, $file;
  10.                    @lines=<FILE>;;
  11.                    close FILE;
  12.                    for $line (@lines){
  13.                                      if($line=~s/www.163.com/www.888.com/){$seen++;}
  14.                                      }
  15.                                      open FILE, ">;$file";
  16.                                      print FILE @lines;
  17.                                      close FILE;
  18.              print "\nFound in $File::Find::name\n" if $seen >; 0;

  19.                  }
  20.                }
复制代码

我在网上搜集了以下,基本上就能满足替换要求!
Because of this words:
"This script can be used as a starting point for more useful or powerful scripts. Readers are encouraged to look up this kind of editing in the Perl cookbook."
我决定用下面的但是看不懂,麻烦大家看看,帮忙解释以下(详细一点啊
  1. use File::Find;
  2. @ARGV = ('.') unless @ARGV;
  3. $dir = shift @ARGV;
  4. find(\&edits, $dir);
  5. sub edits() {
  6. return unless -f;        #skip directories
  7. $seen = 0;
  8. $file = $_;
  9. #Uncomment next line if you want multi-line edits
  10. #undef $/;
  11. local $^I=".backup";
  12. #Warning - heavy magic here
  13. local @ARGV = ($file);
  14. while(<>;) {
  15. #Remember to use the s option if doing multiline edits
  16. $seen++ if s/Lesson/Chapter/;
  17. print;
  18. }
  19. print "Found in $File::Find::name\n" if $seen >; 0;
  20. #Comment out if you want to keep the backup
  21. #unlink $file.".backup";
  22. }
复制代码

谢谢

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-09-05 19:48 |只看该作者

file::find例子&&求助


  1. use File::Find;

  2. #如果沒有argument傳入..則設定default的 目前目錄 "."
  3. @ARGV = ('.') unless @ARGV;
  4. #取出第一個目錄..
  5. $dir = shift @ARGV;
  6. #--呼叫File::Find中的find subroutine...並傳入 ref to subroutine
  7. # 與 目錄 ,其中edits是callback method..
  8. find(\&edits, $dir);

  9. #---設定edits的內容
  10. sub edits() {
  11. #---find會在每次一查到一個file時就呼叫edits..
  12. #--略過非files的文件..
  13. return unless -f;        #skip directories
  14. $seen = 0;
  15. #--取得filename
  16. $file = $_;
  17. #Uncomment next line if you want multi-line edits
  18. #---將 "\n"當成分行符號的設定關閉..
  19. #undef $/;
  20. #--設定自動backup的附加文件名稱
  21. #--例如原來文件abc..備份文件abc.backup
  22. local $^I=".backup";
  23. #Warning - heavy magic here
  24. #--利用local的特定..暫時將argument設定成 find所找到的file name
  25. local @ARGV = ($file);

  26. #--從STDIN中讀取數值..就是把第一個argument讀進來
  27. while(<>;) {
  28. #Remember to use the s option if doing multiline edits
  29. #----替換掉STDIN中的Lesson成為Chapter..
  30. $seen++ if s/Lesson/Chapter/;
  31. #--print 出來改完後結果..
  32. print;
  33. }
  34. #--因為$seen>;0..表示s///有改到file..因此可以知道有找到 Lesson的file..
  35. print "Found in $File::Find::name\n" if $seen >; 0;
  36. #Comment out if you want to keep the backup
  37. #unlink $file.".backup";
  38. }
复制代码

大概像這樣..詳情還是要看一下CPAN上的說明...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP