【问题标题】:Perl regularexpression match if found print whole linePerl 正则表达式匹配如果找到打印整行
【发布时间】:2014-02-25 23:34:11
【问题描述】:

我有很多行

  Hi i need to work for
  This place
  Which adds me
  More value in this
   wolrd
  Where i can explore my technical skills

我正在搜索单词添加me =~ /adds/

如果找到匹配项,我必须打印整行“这增加了我”

如何在 perl 中做到这一点

【问题讨论】:

  • 这些行在文件中????

标签: regex perl


【解决方案1】:
perl -lne 'print if(/\badds\b/)' your_file

测试here

【讨论】:

  • 这个函数有什么作用? @维杰
  • 你说的是哪个函数?
【解决方案2】:
while (<STDIN>) {
  if($_ =~ m/adds me/) { ##this worked for me
    print $_;}
}

【讨论】:

    【解决方案3】:

    使用perl 命令:

    perl -ne '/adds/ && print $_' file
    

    输出:

      Which adds me
    

    在 script.pl 中:

    while (<>) {
        print if (/adds/);
    }
    

    然后调用脚本:

    perl script.pl file
    

    【讨论】:

    • 如何在没有 oneliner @vanda 的情况下做到这一点
    • @user3349964 看@脚本版本。
    【解决方案4】:

    我想知道是否可以使用选项 -p 解决问题。终于找到了这个oneliner

    perl -pe 'goto LINE unless /adds/;' filename
    

    【讨论】:

      【解决方案5】:

      如果在文件中

      open(FH,'filename");
      
      while(<FH>){
      
          if($_=~/adds/){
              print $_;
          }
      }
      

      【讨论】:

        【解决方案6】:
        while (<FILE>) {
        
           if (/adds/) { print $_;}
        }
        

        【讨论】:

        • 请更好地解释你的答案
        猜你喜欢
        • 2015-02-24
        • 1970-01-01
        • 2017-01-20
        • 1970-01-01
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多