Grep 匹配整个电子邮件多行

huangapple go评论62阅读模式
英文:

Grep Match entire email multiple lines

问题

I want the output to be:

./j/r:franky@list.ru:Test2
./j/d:jdmq77@hotmail.com:Test3
./w/i:willow@gmail.com@gmail.com:Test6
英文:

I have two files:

I have this fist file:

./j/n:jwillow@gmail.com:Test1
./n/o:nanofranky@list.ru:Test2
./j/r:franky@list.ru:Test2
./j/d:jdmq77@hotmail.com:Test3
./x/s:xsebastianmenendez@hotmail.com:Test4
./r/s:rsebastianmenendez@hotmail.com:Test5
./w/i:willow@gmail.com@gmail.com:Test6

And I want it to match this second file, but the whole email, not just the appearance of the word:

willow@gmail.com
franky@list.ru
jdmq77@hotmail.com
sebastianmenendez@hotmail.com

So I want the output to be:

./j/r:franky@list.ru:Test2
./j/d:jdmq77@hotmail.com:Test3
./w/i:willow@gmail.com@gmail.com:Test6

I try with grep -f, but, since the words appear in the email, I get all the lines of the first file, not just the ones that matches with the whole email.

答案1

得分: 4

Use the -w argument.
Output:
./w/i:willow@gmail.com@gmail.com:Test6

英文:

Use the -w argument.

grep -w 'willow@gmail.com' file.txt

Output:

./w/i:willow@gmail.com@gmail.com:Test6

Bash script example to compare two files:

#!/bin/bash

strings_file="search_for.txt"
input_file="in.txt"

grep -w -F -f "$strings_file" "$input_file"

答案2

得分: 3

你可以使用以下命令:

grep -wFf second_file.txt first_file.txt

这里,second_file.txt 是包含你想匹配的电子邮件地址的文件,而 first_file.txt 是你想在其中搜索匹配项的文件。
-w 选项确保只匹配整个单词,而 -F 选项将搜索模式视为固定字符串(不是正则表达式)。

英文:

You may use:

grep -wFf second_file.txt first_file.txt

Here, second_file.txt is the file containing the email addresses you want to match, and first_file.txt is the file you want to search for matches in.
The -w option ensures that only whole words are matched, and the -F option treats the search pattern as a fixed string (not a regular expression though).

答案3

得分: 0

这可能是你想要的,使用任何awk:

NR == FNR { tgts[$1]; next }
{ email=$0; sub(/[^:]+:/,"",email); sub(/:[^:]+$/,"",email) }
email in tgts

它没有产生预期的输出,因为我认为在file1中存在无效的电子邮件地址willow@gmail.com@gmail.com并匹配file2中的willow@gmail.com可能是问题中的拼写错误。

英文:

This might be what you want, using any awk:

$ awk '
    NR == FNR { tgts[$1]; next }
    { email=$0; sub(/[^:]+:/,"",email); sub(/:[^:]+$/,"",email) }
    email in tgts
' file2 file1
./j/r:franky@list.ru:Test2
./j/d:jdmq77@hotmail.com:Test3

It doesn't produce the expected output because I think having the invalid email address willow@gmail.com@gmail.com present in file1 and match willow@gmail.com from file2 is probably a typo in the question.

答案4

得分: 0

两者基本上是做相同的事情,只是在语法上有细微的变化。

  • 第一个是通过"short-circuit"布尔AND &&OR ||运算符实现的。

  • 第二个是相同的概念,但是使用if-(?)-then-(:)-else三元操作符实现。

> gawk 'FNR < NR && $2 in __ || __[$_] FS=:
> mawk -F: 'FNR < NR ? $2 in __ : __[$_]

<( printf '%s'
       'willow@gmail.com
        franky@list.ru
        jdmq77@hotmail.com
        sebastianmenendez@hotmail.com' ) 

<( printf '%s' 
   './j/n:jwillow@gmail.com:Test1
    ./n/o:nanofranky@list.ru:Test2
    ./j/r:franky@list.ru:Test2
    ./j/d:jdmq77@hotmail.com:Test3
    ./x/s:xsebastianmenendez@hotmail.com:Test4
    ./r/s:rsebastianmenendez@hotmail.com:Test5
    ./w/i:willow@gmail.com:Test6' )

     1	./j/r:franky@list.ru:Test2
     2	./j/d:jdmq77@hotmail.com:Test3
     3	./w/i:willow@gmail.com:Test6
英文:

both does basically the same thing, with minor variation in syntax

  • 1st one via "short-circuit" boolean AND && and OR ||

  • 2nd one is same concept but via the if-(?)-then-(:)-else ternary operator instead

> gawk 'FNR < NR && $2 in __ || __[$]' FS=:
> mawk -F: 'FNR < NR ? $2 in __ : __[$
]'

&lt;( printf &#39;%s&#39;
       &#39;willow@gmail.com
        franky@list.ru
        jdmq77@hotmail.com
        sebastianmenendez@hotmail.com&#39; ) 

&lt;( printf &#39;%s&#39; 
   &#39;./j/n:jwillow@gmail.com:Test1
    ./n/o:nanofranky@list.ru:Test2
    ./j/r:franky@list.ru:Test2
    ./j/d:jdmq77@hotmail.com:Test3
    ./x/s:xsebastianmenendez@hotmail.com:Test4
    ./r/s:rsebastianmenendez@hotmail.com:Test5
    ./w/i:willow@gmail.com:Test6&#39; )

     1	./j/r:franky@list.ru:Test2
     2	./j/d:jdmq77@hotmail.com:Test3
     3	./w/i:willow@gmail.com:Test6

huangapple
  • 本文由 发表于 2023年5月18日 01:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定