为什么grep-wf返回的结果与grep-w不同;第1行“;

为什么grep-wf返回的结果与grep-w不同;第1行“;,grep,Grep,我正在尝试学习如何使用grep,但我不明白为什么当我使用一个字符串对一组文件进行grep时,我得不到与使用具有相同字符串的文件时相同的结果 在我的主目录中: [匹配/strings.txt] 1\.1\.1\.1 2\.2\.2\.2 --strings.txt包含。[dot]转义ip,不带前导或尾随空格 [ips1、ips2、ips3、ips4] ... massive lists of id's and ip's ... 1234 1.1.2.3 1222

我正在尝试学习如何使用grep,但我不明白为什么当我使用一个字符串对一组文件进行grep时,我得不到与使用具有相同字符串的文件时相同的结果

在我的主目录中:

[匹配/strings.txt]

    1\.1\.1\.1
    2\.2\.2\.2
--strings.txt包含。[dot]转义ip,不带前导或尾随空格

[ips1、ips2、ips3、ips4]

   ... massive lists of id's and ip's ...
1234    1.1.2.3
1222    1.1.2.4
当我跑步时:

    $ grep -w "1\.1\.1\.1" ips*
我每次都得到100%的正确匹配。 ips1:11111.1.1.1 ips2:11111.1.1.1

当我跑步时:

    $ grep -wf matches/strings.txt ips*
我得到了一包正确的和不正确的匹配 ips1:11121.1.1.2 ips1:5111.1.1.1 ips1:15115.1.1.1

我不知道我是否只是使用了错误的工具来做这件事,或者我只是没有使用正确的选项。我还尝试了以下选项:

格雷普-Ff grep-wFf grep-wHFf

所有这些都没有给我与grep-w“1.1.1.1”相同的结果,我尝试在我的strings.txt文件中的ip周围加引号,这没有什么区别,我得到了相同的正确和错误匹配的混合包。我也尝试过只匹配一个文件,而不是一次匹配所有IP*,我得到了相同的混合结果

这里是一些添加了额外换行符的真实数据。。。真正的strings.txt在点转义IP之间没有空行,只是格式设置太长,无法正确显示

grep-wf match/strings.txt ips*|更多

ips1:01100.0.109.83

ips1:0100.10.42.110

ips1:0100.1.100.194

ips1:0100.1.111.51

d:~$cat match/strings.txt

“100\.4\.144\.28”

“205\.154\.246\.79”

“172\.58\.23\.203”

“172\.58\.21\.242”

“172\.58\.21\.186”

“172\.58\.23\.212”

“172\.58\.21\.32”

“104\.174\.15\.38”

“67\.87\.1\.146”

“128\.177\.161\.177”

“172\.58\.21\.228”

“172\.58\.23\.19”

“172\.56\.38\.234”

“173\.46\.78\.139”

“100\.4\.149\.40”

“107\.195\.45\.250”

“205\.197\.242\.184”

“128\.177\.161\.161”

在strings.txt中没有任何引号

~$grep-wf match/strings.txt ips*|更多 ips1:0100.34.7.21

ips1:0100.35.114.199

ips1:0100.35.148.65

ips1:0100.35.161.40

ips1:0100.35.195.180

ips1:0 100.35.211.106

ips1:0 100.35.216.172

:~$cat match/strings.txt

100\.4\.144\.28

205\.154\.246\.79

172\.58\.23\.203

172\.58\.21\.242

172\.58\.21\.186

172\.58\.23\.212

172\.58\.21\.32

104\.174\.15\.38

67\.87\.1\.146

128\.177\.161\.177

172\.58\.21\.228

172\.58\.23\.19

172\.56\.38\.234

173\.46\.78\.139

100\.4\.149\.40

107\.195\.45\.250

205\.197\.242\.184

128\.177\.161\.161

以下是ips1中的实际数据示例(添加了额外的换行符):

从该文件中删除所有内容并一次添加一行,修复了该问题。我不知道为什么从Excel文档中复制和粘贴可能会导致一些奇怪的事情,这是我最好的猜测

:~$grep-w“100\.4\.144\.28”ips*| wc-l

十一,

~$grep-wf match/strings.txt ips*| wc-l

十一,

$grep-wf match/strings.txt ips*| wc-l

二十九

$grep-w“107\.195\.45\.250”ips*| wc-l

十八

$grep-wf match/strings.txt ips*| wc-l

37

$grep-w“172\.58\.23\.19”ips*| wc-l


8

您确定您的文件包含您认为它们的功能吗?我只是试了一下。我和你有什么不同

$ cat strings.txt
1\.1\.1\.1
$ cat ips.txt
1111 1.1.1.1
2222 2.2.2.2
1112 1.1.1.2
2221 2.2.2.1
1111b 1.1.1.1
2222b 2.2.2.2
$ grep -w "1\.1\.1\.1" ips*
1111 1.1.1.1
1111b 1.1.1.1
$ grep -wf strings.txt ips*
1111 1.1.1.1
1111b 1.1.1.1

我不确定这是否会影响您的测试,但我有4个文件ips1、ips2、ips3、ips4,谢谢。。。我不知道我的文件“出了什么问题”,但当我从文本编辑器中删除所有内容并开始一次添加一行时,我开始得到一致正确的结果!谢谢。strings.txt中有双引号吗?这应该无法匹配ips文件格式中的任何内容。我得到了类似的错误结果,但没有引号=/
$ cat strings.txt
1\.1\.1\.1
$ cat ips.txt
1111 1.1.1.1
2222 2.2.2.2
1112 1.1.1.2
2221 2.2.2.1
1111b 1.1.1.1
2222b 2.2.2.2
$ grep -w "1\.1\.1\.1" ips*
1111 1.1.1.1
1111b 1.1.1.1
$ grep -wf strings.txt ips*
1111 1.1.1.1
1111b 1.1.1.1