Perl从文本文件中按关键字提取数据
来源:互联网 发布:少年西游记手游源码 编辑:程序博客网 时间:2024/06/08 16:35
#!/usr/bin/perl -w
#require "convertword.pl";
use strict;
use warnings;
use Cwd;
use File::Spec::Functions qw( catfile );
# 转换格式
print "转换Word为文本文件...\n";
#&convertWord();
`perl convertword.pl`;
# 关键字设置
#my @KEY_WORD = ("申请部门","申请时间","申请人");
my @KEY_WORD = ();
# 获取当前目录的完整路径信息。
my $currentDir = getcwd;
print "当前目录: ".$currentDir."\n";
# 配置文件
my $cfgFile = "$currentDir/关键字.txt";
# 设置源文件夹
my $SRC_PATH = "$currentDir/TXT/";
# 设置临时文件夹
my $TMP_PATH = "$currentDir/Temp/";
# 设置目标文件夹
my $DEST_PATH="$currentDir/Result/";
# 如果目标文件夹不存在,则创建文件夹
mkdir( $DEST_PATH, 0777 ) if ( !-d $DEST_PATH);
opendir TEMP, ${SRC_PATH} or die "无法打开".$SRC_PATH."目录,请检查一下目录是否存在!";
@KEY_WORD=&readConfig();
# 读取目录下所有文件
my @filelist = readdir TEMP;
my $srcFile='';
my $srcFileBackup='';
my $destFile=$DEST_PATH.'Result.csv';
my $dateflag=0;
my $ownerflag=0;
my $date='';
my $owner='';
my $count=0;
my $flag=0;
my $line='';
# 如果目标文件夹不存在,则创建文件夹
mkdir($DEST_PATH, 0777) if(! -d $DEST_PATH);
# 删除并新建临时目录
deldir($TMP_PATH) if( -d $TMP_PATH);
mkdir($TMP_PATH, 0777) if(! -d $TMP_PATH);
open(FH_PS, ">$destFile") or die "无法打开文件: $destFile <$!>";
# 输出文件头
my $header = (join ',', @KEY_WORD).",";
print FH_PS $header."\n";
foreach (@filelist) {
next if /^\./;
$count=$count+1;
$srcFile=$SRC_PATH.$_;
$srcFileBackup=$TMP_PATH.$_.'.bkp';
print "\n";
open(FH_B,">$srcFileBackup") or die "无法打开文件: $srcFileBackup";
open(FH,$srcFile) or die "无法打开文件: $srcFile";
while(<FH>){
#chomp;
s/\r/\|/g;
s/\n\n/\n/g;
#s/\n\n/\n/g;
print FH_B $_;
#s/提交人\r/\n提交人|/;
#s/\r提交单位\r/\n提交单位|/;
}
close(FH_B);
close(FH);
open(FH_B,$srcFileBackup) or die "无法打开文件: $srcFileBackup";
my $result='';
while(<FH_B>){
chomp;
$line = $_;
foreach my $key(@KEY_WORD){
# 根据关键字匹配相应的值
if($line=~/$key\|(.+?)\|(.+)/){
my $value = $1;
print $count.":".$key."|".$value."\n";
$result = $result.$value.",";
}
}
}
print FH_PS $result."\n";
close(FH_B);
}
close(FH_PS);
# 删除临时目录
deldir($TMP_PATH) if( -d $TMP_PATH);
print "All files are processed.\nTotal file processed: $count\n";
sub readConfig(){
# 读取配件文件 - START
open(FH_CONFIG, "<$cfgFile") || die "无法打开配置文件 : $cfgFile <$!>";
my @words=();
while(<FH_CONFIG>) {
chomp;
next if /^#/; # skip comments
next if /^\s*$/; # skip empty lines
if (/^\s*(.+),(.+)\s*$/){
@words = split (/,/,$_);
last;
}
}
print "@words\n";
close FH_CONFIG;
trimList(@words);
# 读取配件文件 - END
}
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Perl trim function to remove whitespace from the start and end of the string
sub trimList($)
{
my @myList = ();
foreach(@_){
push (@myList, trim($_));
}
return @myList;
}
sub deldir {
my($del_dir)=$_[0];
my(@direct);
my(@files);
opendir (DIR2,"$del_dir");
my(@allfile)=readdir(DIR2);
close (DIR2);
foreach (@allfile){
if (-d "$del_dir/$_"){
push(@direct,"$_");
}
else {
push(@files,"$_");
}
}
my $files=@files;
my $direct=@direct;
if ($files ne "0"){
foreach (@files){
unlink("$del_dir/$_");
}
}
if ($direct ne "0"){
foreach (@direct){
&deldir("$del_dir/$_") if($_ ne "." && $_ ne "..");
}
}
rmdir ("$del_dir");
}
#require "convertword.pl";
use strict;
use warnings;
use Cwd;
use File::Spec::Functions qw( catfile );
# 转换格式
print "转换Word为文本文件...\n";
#&convertWord();
`perl convertword.pl`;
# 关键字设置
#my @KEY_WORD = ("申请部门","申请时间","申请人");
my @KEY_WORD = ();
# 获取当前目录的完整路径信息。
my $currentDir = getcwd;
print "当前目录: ".$currentDir."\n";
# 配置文件
my $cfgFile = "$currentDir/关键字.txt";
# 设置源文件夹
my $SRC_PATH = "$currentDir/TXT/";
# 设置临时文件夹
my $TMP_PATH = "$currentDir/Temp/";
# 设置目标文件夹
my $DEST_PATH="$currentDir/Result/";
# 如果目标文件夹不存在,则创建文件夹
mkdir( $DEST_PATH, 0777 ) if ( !-d $DEST_PATH);
opendir TEMP, ${SRC_PATH} or die "无法打开".$SRC_PATH."目录,请检查一下目录是否存在!";
@KEY_WORD=&readConfig();
# 读取目录下所有文件
my @filelist = readdir TEMP;
my $srcFile='';
my $srcFileBackup='';
my $destFile=$DEST_PATH.'Result.csv';
my $dateflag=0;
my $ownerflag=0;
my $date='';
my $owner='';
my $count=0;
my $flag=0;
my $line='';
# 如果目标文件夹不存在,则创建文件夹
mkdir($DEST_PATH, 0777) if(! -d $DEST_PATH);
# 删除并新建临时目录
deldir($TMP_PATH) if( -d $TMP_PATH);
mkdir($TMP_PATH, 0777) if(! -d $TMP_PATH);
open(FH_PS, ">$destFile") or die "无法打开文件: $destFile <$!>";
# 输出文件头
my $header = (join ',', @KEY_WORD).",";
print FH_PS $header."\n";
foreach (@filelist) {
next if /^\./;
$count=$count+1;
$srcFile=$SRC_PATH.$_;
$srcFileBackup=$TMP_PATH.$_.'.bkp';
print "\n";
open(FH_B,">$srcFileBackup") or die "无法打开文件: $srcFileBackup";
open(FH,$srcFile) or die "无法打开文件: $srcFile";
while(<FH>){
#chomp;
s/\r/\|/g;
s/\n\n/\n/g;
#s/\n\n/\n/g;
print FH_B $_;
#s/提交人\r/\n提交人|/;
#s/\r提交单位\r/\n提交单位|/;
}
close(FH_B);
close(FH);
open(FH_B,$srcFileBackup) or die "无法打开文件: $srcFileBackup";
my $result='';
while(<FH_B>){
chomp;
$line = $_;
foreach my $key(@KEY_WORD){
# 根据关键字匹配相应的值
if($line=~/$key\|(.+?)\|(.+)/){
my $value = $1;
print $count.":".$key."|".$value."\n";
$result = $result.$value.",";
}
}
}
print FH_PS $result."\n";
close(FH_B);
}
close(FH_PS);
# 删除临时目录
deldir($TMP_PATH) if( -d $TMP_PATH);
print "All files are processed.\nTotal file processed: $count\n";
sub readConfig(){
# 读取配件文件 - START
open(FH_CONFIG, "<$cfgFile") || die "无法打开配置文件 : $cfgFile <$!>";
my @words=();
while(<FH_CONFIG>) {
chomp;
next if /^#/; # skip comments
next if /^\s*$/; # skip empty lines
if (/^\s*(.+),(.+)\s*$/){
@words = split (/,/,$_);
last;
}
}
print "@words\n";
close FH_CONFIG;
trimList(@words);
# 读取配件文件 - END
}
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Perl trim function to remove whitespace from the start and end of the string
sub trimList($)
{
my @myList = ();
foreach(@_){
push (@myList, trim($_));
}
return @myList;
}
sub deldir {
my($del_dir)=$_[0];
my(@direct);
my(@files);
opendir (DIR2,"$del_dir");
my(@allfile)=readdir(DIR2);
close (DIR2);
foreach (@allfile){
if (-d "$del_dir/$_"){
push(@direct,"$_");
}
else {
push(@files,"$_");
}
}
my $files=@files;
my $direct=@direct;
if ($files ne "0"){
foreach (@files){
unlink("$del_dir/$_");
}
}
if ($direct ne "0"){
foreach (@direct){
&deldir("$del_dir/$_") if($_ ne "." && $_ ne "..");
}
}
rmdir ("$del_dir");
}
0 0
- Perl从文本文件中按关键字提取数据
- 从文本文件提取数据到数据库指定的字段中
- 利用Perl切片从列表数据中提取关键…
- 从文本文件中读取数据
- 从文本文件中读取数据
- DataExtraction 本工具可以从文件中按关键字提取数据和排序功能 [版本 0.88] BY Hong Wenjun
- php:从网页中提取关键字
- 从modelsim中提取数据
- 从Excel中提取数据
- scikit-learn:从文本文件中提取特征(tf、idf)
- perl提取表格数据
- perl数据提取
- C++从文本文件中读一行数据
- 从文本文件中读取数据画图
- 从Oracle中导出数据到文本文件
- 43节从文本文件中读入数据!
- python 从文本文件中解析数据
- 从百万关键字中提取前K个关键字
- Opencv SURF 图像匹配
- 《高效学习OpenGL》 之 累积缓冲区 glAccum()
- ubuntu12.04LTS 搭建android 开发环境
- 动态规划-矩阵连乘
- GDB详解
- Perl从文本文件中按关键字提取数据
- iOS7中UITextView contentsize改变时机
- 2014-03-04安装eclispe cdt
- 最长回文子字符串 还要更简单的方法
- 2014-03-04
- 皇冠3D成型與最流行使用的材料
- Q2.3
- Windows7 64位系统搭建Cocos2d-x-2.2.1最新版以及Android交叉编译环境(详细教程)
- 从hadoop框架与MapReduce模式中谈海量数据处理