+ -
当前位置:首页 → 问答吧 → 请教各位高手2个文本替换功能的实现。我怎么替换都弄不过来。

请教各位高手2个文本替换功能的实现。我怎么替换都弄不过来。

时间:2010-09-14

来源:互联网

昨天把文档替换的任务完成了,没想到,又来两个新要求,把我整蒙了。一直没试成功。
请各位高手帮我看看怎么写?替换我完成了,就是不知道怎么把原处保留,把替换的写在下面。
例子是这样:
hello
  goodmorning
  I like the weather
  monday (
  .one (shoping)
  .two (washing)
  .three (cooking)
  .four (sleep)
  );
  I eat apple
  monday (
  .one (working)
  .two (party)
  .three (mom is coming)
  .four (have a lunch)
  );
  ok,let'go~
    要求是,把monday 变成 workday 并把下面的 .two (shoping) 变成 .second  (~shoping)。
   别人给我提的新要求是,用/*       */把原处注释掉,然后把修改的写在下面。
  成了
*/
     monday (
  .one (shoping)
  .two (washing)
  .three (cooking)
  .four (sleep)
  );
*/
       workday (
  .one (shoping)
  .second (shoping)
  .three (cooking)
  .four (sleep)
);

   无论我怎么换,脚本都是一行一行操作,把要改的地方注释掉,然后把替换内容写在下面的操作,老成功不了。
请教各位高手,我该怎么弄呢?


  另外一个问题
  我为了检查方便,用
  if (s/\.two/\second)  {
   $rptwo ++; }
最后 print "\//rptwo = $rptwo\n
来表示我修改了多少个。

  但现在,我有40个文件,我想把最后这行记录,都存到一个叫xxcheck的文件里
  内容是:
  被替换的文件名1:  rptwo 替换次数

  被替换的文件名2:  rptwo 替换次数

  我用原来的批处理方式,可以群体替换,但是一直想不出,该如何生成这样一个文件。
  不知哪个朋友能指点一下,多谢啦。

作者: xixizhihua   发布时间: 2010-09-14

[oracle@imeg02 zgw]$ perl -pe 'BEGIN{$/=";"}s:\s+monday(.*)\.two\s+\((\w+)\)(.*):\n/*$&\n*/\n  workday$1.second (~$2)$3:s' file
hello
  goodmorning
  I like the weather
/*
  monday (
  .one (shoping)
  .two (washing)
  .three (cooking)
  .four (sleep)
  );
*/
  workday (
  .one (shoping)
  .second (~washing)
  .three (cooking)
  .four (sleep)
  );
  I eat apple
/*
  monday (
  .one (working)
  .two (party)
  .three (mom is coming)
  .four (have a lunch)
  );
*/
  workday (
  .one (working)
  .second (~party)
  .three (mom is coming)
  .four (have a lunch)
  );

作者: 99超人   发布时间: 2010-09-14

[oracle@imeg02 zgw]$ perl -i -pe '$file{$ARGV}+=s/\.two/.second/;END{foreach (keys %file) {print $_,",",$file{$_},"\n"}}' *.txt >xxcheck
[oracle@imeg02 zgw]$ cat xxcheck
3.txt,5
1.txt,2
2.txt,5

作者: 99超人   发布时间: 2010-09-14