使用Perl批量读取文件最后行

面对成百上千个文件,有时我们需要查看它的最后行,单个文件打开将耗费大量时间,而通过Perl提取出最后行,将快速的帮助我们处理繁琐的事务。

特性


整个目录完全遍历,自动提取最后行

提取出的文件结构


Perl代码


#!/usr/bin/perl
use warnings; ###########################################
#./readlast_line.pl log/
########################################### my $indir = $ARGV[0]; opendir DIR, $indir or die "Connot open $indir: $!"; unlink "temp_line";
open(OUTFILE, ">>temp_line") || die ("Could not open file temp_line ! \n"); foreach my $file (sort readdir DIR){ #遍历目录下的文件
if($file =~ /^\./){
next;
}
#print "$indir$file\n";
print OUTFILE "\t\t $indir$file \t\t \n"; #输出文件名及其路径 my $last_line = `tail -10 $indir$file`; #反引号捕获
print OUTFILE "$last_line \n";
print OUTFILE "\n\n\n"; #分割不同文件之间的换行
} close OUTFILE;
rename 'temp_line','filelast_line'; print "\nOutput file: filelast_line\n";

最新文章

  1. CentOS7下安装mysql5.6修改字符集为utf8并开放端口允许远程访问
  2. ES6之const命令
  3. 使用canvas实现擦玻璃效果---转载
  4. Eclipse学习记录
  5. 分享我设计的iOS项目目录结构
  6. JavaScriptResult用法
  7. OOA、OOD、OOP
  8. Eclipse Removing obsolete files from server 问题
  9. 转 intValue()的用法
  10. ef mysql
  11. IT公司PM沟通那儿些事(一)
  12. python基础 ---- 安装
  13. SQL IN 操作符
  14. Spring SpringBoot和SpringCloud的关系
  15. dd 命令的使用
  16. 自定义MVC框架之工具类-文件上传类
  17. linq 根据指定条件返回集合中不重复的元素
  18. SQL Server SqlCacheDependency 缓存依赖
  19. matlab一些函数
  20. 4G模块ME3760_V2的拨号过程

热门文章

  1. *android抓包工具——fiddler抓包分析api接口
  2. 通过wireshark,以及python代码收发邮件,了解smtp协议,pop协议工作过程
  3. <memory>(包括了auto_ptr,shared_ptr等各种指针)
  4. Oracle Sqlplus中上下键出现^[[A乱码问题
  5. php中ajax使用实例
  6. jodd-cache集锦
  7. AJAX - 封装的传参改为传入对象 XML JSON 数据格式
  8. 如何优雅的写UI——(1)MFC六大核心机制-程序初始化
  9. Android: 分页浏览的利器 android View Pager
  10. 86.八千万qq密码按相似度排序并统计密码出现次数,生成密码库