Chinaunix

标题: 怎样在perl脚本中,while模块内,不打印的情况下,删除原文件的当前行 [打印本页]

作者: freesiaSS    时间: 2017-03-18 17:22
标题: 怎样在perl脚本中,while模块内,不打印的情况下,删除原文件的当前行
如题,
我有个问题,百试不得其解。请大神赐教
比如有这样一个文件 ss.txt
内容是:
123
34556
556868
346
3345657
33434
2233
2325
问题是,在不打印的情况下,我想在原文件中删除346这一行。
请帮我补填一下下面的脚本。。感谢!
#perl test.pl ss.txt
use warnings;
open IN, "<$ARGV[0]" || die $!;
$/="\n";
<IN>;
while(<IN>){
    if($_=~/346/){删除当前行$_}
}
close(IN);

作者: 华小飞_Perl    时间: 2017-03-19 20:15
  1. #!/usr/bin/perl

  2. use warnings;
  3. use strict;

  4. while (<>) {
  5.         chomp;
  6.         next if (s/^346$//m);
  7.         print $_, "\n";
  8. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2