在文本文件中使用列表查找和替换? [英] Find and replace using a list in text file?

查看:151
本文介绍了在文本文件中使用列表查找和替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可能,但确定希望它是。



我在文件backup.xml中有这行766次:

  ***托管服务

然后我有文件 list.txt ,其中包含766行。我需要用 list.txt 中的每个766行的内容替换 *** - 它需要



感谢您提供任何帮助!

解决方案

思路:

 文件)
如果你(F)ind B行改变
读取L(ist文件)的下一行
change
打印行到R(结果文件)

计划


$ b b

  read_open B 
read_open L
write_open R
while(从B开始)
if(F){
读取替换L
更改行
}
打印行到R
}
关闭R,L,B

实施I(read_open,循环,查看B)

  use strict; 
使用警告;
使用英语qw(-no_match_vars);

my $ bfn ='../data/AA-backup-xml';
open my $ bfh,'<',$ bfn or die无法读取'$ bfn':$ OS_ERROR;
while(my $ line =< $ bfh>){
print $ line;
}
close $ bfh或die无法读取close$ bfn':$ OS_ERROR;

输出:

 code> perl 01.pl 
whatever
whatever
***托管服务
无论
无论
无论
***托管服务
无论
无论
***托管服务
无论
无论
无论
***托管服务



实施II(读/写,F,替换,第一个结果):

 使用Modern :: Perl; 
使用英语qw(-no_match_vars);

my $ bfn ='../data/AA-backup-xml';
open my $ bfh,'<',$ bfn or die无法读取'$ bfn':$ OS_ERROR;
my $ lfn ='../data/AA-list.txt';
open my $ lfh,'<',$ lfn or die无法读取'$ lfn':$ OS_ERROR;
my $ rfn ='../data/AA-result';
open my $ rfh,'>',$ rfn or die无法写入打开'rlfn':$ OS_ERROR;
while(my $ line =< $ bfh>){
if($ line =〜/ \ * {3} /){
my $ rpl =< $ lfh> ;
$ rpl = substr($ rpl,0,3);
$ line =〜s / \ * {3} / $ rpl /;
}
print $ rfh $ line;
}
close $ rfh or dieCan not write close$ rfn':$ OS_ERROR;
close $ lfh或die无法读取close'$ lfn':$ OS_ERROR;
close $ bfh或die无法读取close$ bfn':$ OS_ERROR;

输出:

 code> type ..\data\AA-result 
whatever
whatever
001托管服务
无论
无论
无论
002托管服务
无论
无论
003托管服务
无论
无论
无论
004托管服务

如果这对你不起作用(也许我错误地猜到B的结构或F策略太天真) ,然后发布B,L和R的代表性样本。


I'm not even sure if this is possible, but sure am hoping that it is.

I have this line 766 times in the file backup.xml:

*** Hosting Services

I then have the file list.txt which contains 766 lines in it. I need to replace *** with the contents of each of the 766 lines in list.txt - and it needs to be in the same order if at all possible.

Thanks in advance for any help!

解决方案

Idea:

loop over the lines of the B(ackup file)
  if you (F)ind a B-line to change
     read the next line of the L(ist file)
     change
  print the line to R(result file)

Plan:

read_open B
read_open L
write_open R
while (line from B)
  if (F) {
    read replacemment from L
    change line
  }
  print line to R
}
close R, L, B

Implementation I (read_open, loop, look at B):

use strict;
use warnings;
use English qw(-no_match_vars);

my $bfn = '../data/AA-backup-xml';
open my $bfh, '<', $bfn or die "Can't read open '$bfn': $OS_ERROR";
while (my $line = <$bfh>) {
        print $line;
}
close $bfh or die "Can't read close '$bfn': $OS_ERROR";

output:

perl 01.pl
whatever
whatever
*** Hosting Services
whatever
whatever
whatever
*** Hosting Services
whatever
whatever
*** Hosting Services
whatever
whatever
whatever
*** Hosting Services

Implementation II (read/write, F, replace, first result):

use Modern::Perl;
use English qw(-no_match_vars);

my $bfn = '../data/AA-backup-xml';
open my $bfh, '<', $bfn or die "Can't read open '$bfn': $OS_ERROR";
my $lfn = '../data/AA-list.txt';
open my $lfh, '<', $lfn or die "Can't read open '$lfn': $OS_ERROR";
my $rfn = '../data/AA-result';
open my $rfh, '>', $rfn or die "Can't write open 'rlfn': $OS_ERROR";
while (my $line = <$bfh>) {
    if ($line =~ /\*{3}/) {
        my $rpl = <$lfh>;
        $rpl = substr($rpl, 0, 3);
        $line =~ s/\*{3}/$rpl/;
    }
    print $rfh $line;
}
close $rfh or die "Can't write close '$rfn': $OS_ERROR";
close $lfh or die "Can't read close '$lfn': $OS_ERROR";
close $bfh or die "Can't read close '$bfn': $OS_ERROR";

output:

type ..\data\AA-result
whatever
whatever
001 Hosting Services
whatever
whatever
whatever
002 Hosting Services
whatever
whatever
003 Hosting Services
whatever
whatever
whatever
004 Hosting Services

If this does not 'work' for you (perhaps I mis-guessed the structur of B or the F strategy is too naive), then publish a representative sample of B, L, and R.

这篇关于在文本文件中使用列表查找和替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆