在文件中查找文本的Perl脚本
来源:互联网 发布:淘宝企业店铺靠不靠谱 编辑:程序博客网 时间:2024/05/18 01:17
Windows里没有grep工具,不方便对文件的内容进行查找,随手写了一个Perl脚本,脚本写得不是很简洁,但能工作。
Github: https://gist.github.com/2273079
#!/usr/bin/perl -w############################################################################# File: findtext.pl# Brief: Match text content in a directory## Usage: findtext.pl DIR PATTERN# - DIR work directory# - PATTERN pattern for text search## Author: Thinkhy# Date: 2011-11-8# Changes:#############################################################################use strict;use Encode;use File::Glob ':glob';my $cnt = @ARGV;if ($cnt <= 0){print " Usage: findtext.pl DIR PATTERN- DIR work directory- PATTERN pattern for text search";exit -1;}my $dir = decode("gb2312", $ARGV[0]);my $pattern0 = $ARGV[1];my $pattern = decode("gb2312", $ARGV[1]);print $pattern;chomp($dir);chomp($pattern);$dir =~ s/\\/\//g;print "Search content in directory: ".$dir."\n";print "===================================================\n";print "\n";Find($dir);my $deep = 0;sub Find{my $path = shift;$deep++;my @pathes = listpath($path);my @files = listfile($path);#print join "\n", @pathes;# print join "\n", @files;foreach(@files){my $file = $_;#print "Search".$file."\n";if (isPatternOnFile($file, $pattern)){print $file."\n"}}if ($pathes[0]){foreach(@pathes){Find($_);}}$deep--;}sub listpath{my $path = shift;my @list = bsd_glob "$path/*";#my @list = <$path/*>;my @pathes = grep { -d } @list;return @pathes;}sub listfile{my $path = shift;my @list = bsd_glob "$path/*";#my @list = <$path/*.*>;my @files = grep { -f } @list;return @files;}sub isPatternOnFile{my ($file, $pattern) = @_;my $temp;#read the entire fileopen my $fh, "<:encoding(UTF-8)", $file or die $!;{local $/;$temp = <$fh>;}return 1 if ($temp =~ /$pattern/i) or return 0;}
- 在文件中查找文本的Perl脚本
- Perl 脚本在给定的文件夹目录中递归查找文件
- linux中在多个文件中查找替换文本的shell脚本
- Perl 在指定文件夹中的文件中查找包含指定字符的行
- grep在一个文本中查找符合条件的文本
- Linux在指定文件夹及其子文件夹中查找包含特定文本的文件
- perl脚本去除文件中重复数据
- SQLServer中查找含有某文本的某类型文件
- winform在richTextBox中查找并选择指定的文本
- 使用vim在选中的文本中查找替换
- EmEditor 搜索历史/查找历史的删除/清空 包括在文件中查找的历史删除 文本类型的删除
- 在java中过滤查找的文件
- 在离线的移动硬盘中查找文件
- perl脚本文件的第一行用法
- 一个查找源代码文件中中文字符的脚本
- 各种查找文件的脚本
- 在多个 PDF 中查找文本
- grep在文本中查找内容
- 正则表达式取正文中的汉字
- Android Binder 机制(4)
- ibatis调用oracle的动态sql
- Android中的SQLite的使用
- truncate table table_name
- 在文件中查找文本的Perl脚本
- 3 Things for Efficient Coding——简单 3 步实现高效编程
- DB2存储过程-基础详解
- 通过虚拟机的NAT功能组建网关环境
- Android Binder 机制(5)
- Django+mod_wsgi环境配置攻略
- ContextLoaderListener监听器有什麽用
- Java反射机制深入研究
- vim 复制粘贴