【问题标题】:Only returning matching patterns aginst file with grep仅使用 grep 返回匹配模式 aginst 文件
【发布时间】:2021-02-12 05:33:24
【问题描述】:

我正在尝试使用 grep 将一个电子邮件列表与另一个列表反向分离,以便只返回与这些表达式不匹配的电子邮件。

电子邮件列表如下所示:

   recruitment@madeup.com
   joy@netnoir.net
   hello@nom.com
   mary@itcouldbereal.ac.uk
   thisshouldbe@theonlyone.com

我与之比较的表达式列表是:

   recruitment@
   netnoir.net
   hello@
   "\.ac.\b"

我试过了:

   grep -vif listofexpressions listofemails

我面临的问题是

1.) 没有返回任何内容 2.) .ac。在文件中无法识别,但如果我将其用于

            grep "\.ac.\b" filename 

然后就可以了。

如果我把它改成

        grep -if listofexpressions listofemails

然后大多数不需要转义的表达式都会突出显示,但其他的也会显示出来。

我的预期输出是

      thisshouldbe@theonlyone.com

我确信这很简单,但是在阅读了 grep 的手册页和谷歌搜索之后,我仍然无法解决。

谢谢

【问题讨论】:

  • 为什么mary@itcouldbereal.ac.uk 不在您想要的输出中?
  • 第二个文件中是否有前导空格?
  • 从模式文件中删除 " 字符。在此上下文中(在模式文件中)使用时,它们没有特殊含义,并且匹配文字 "s。

标签: awk grep


【解决方案1】:

对于您显示的示例,您能否尝试以下操作。用 GNU awk 编写和测试。

awk '
FNR==NR{
  arr[$0]
  next
}
{
  found=""
  for(key in arr){
    if(index($0,key)){
      found=1
      next
    }
  }
  if(found==""){
    print
  }
}
' expressions  listemail

说明:为上述添加详细说明。

awk '                        ##Starting awk program from here.
FNR==NR{                     ##Checking condition FNR==NR which will be TRUE when expressions file is being read.
  arr[$0]                    ##Created arr with index of current line here.
  next                       ##next will skip all further statements from here.
}
{
  found=""                   ##Nulliyfing found here.
  for(key in arr){           ##Going through arr elements here.
    if(index($0,key)){       ##Checking if current line is part of key by index.
      found=1                ##Setting found to 1 here.
      next                   ##next will skip all further statements.
    }
  }
  if(found==""){             ##Checking condition if found is NULL then print that line.
    print
  }
}
' expressions  listemails    ##Mentioning Input_files here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多