Reputation: 210
I'm trying to learn how to use grep and I can't figure out why I don't get the same results when I grep a set of files using a string that I get when I use a file with the same exact strings?
in my home directory:
[match/strings.txt]
1\.1\.1\.1
2\.2\.2\.2
--strings.txt contains . [dot] escaped ip with no leading or trailing spaces
[ips1,ips2,ips3,ips4]
... massive lists of id's and ip's ...
1234 1.1.2.3
1222 1.1.2.4
When I run:
$ grep -w "1\.1\.1\.1" ips*
I get 100% correct matches everytime. ips1:1111 1.1.1.1 ips2:1111 1.1.1.1
When I run:
$ grep -wf matches/strings.txt ips*
I get a mixed bag of correct and incorrect matches ips1:1112 1.1.1.2 ips1:5111 1.1.1.1 ips1:1511 5.1.1.1
I don't know if I'm just using the wrong tool to do this or if I'm just not greping with the correct options. I also tried these options:
grep -Ff grep -wFf grep -wHFf
None of those give me the same result as grep -w "1.1.1.1", I have tried putting quotes around the ip's in my strings.txt file and that makes no difference I get the same mixed bag of correct and incorrect matches. I have also tried just matching a single file rather than all ips* at once and I get the same mixed bag of results.
Here is some real data with extra line breaks added... The real strings.txt does not have an empty line between dot escaped IPs it was just taking too long to format it to show up correctly.
grep -wf match/strings.txt ips* | more
ips1: 0 100.0.109.83
ips1: 0 100.10.42.110
ips1: 0 100.1.100.194
ips1: 0 100.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"
without any quotes in strings.txt
~$ grep -wf match/strings.txt ips* | more ips1: 0 100.34.7.21
ips1: 0 100.35.114.199
ips1: 0 100.35.148.65
ips1: 0 100.35.161.40
ips1: 0 100.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
Here is a sample of actual data from ips1 (extra line breaks added):
0 100.0.109.83
0 100.10.42.110
0 100.1.100.194
0 100.1.111.51
0 100.1.138.93
0 100.1.149.82
0 100.11.60.70
0 100.1.187.63
0 100.12.159.242
0 100.12.184.253
0 100.12.190.121
0 100.12.202.213
0 100.12.204.57
0 100.12.223.81
0 100.12.229.63
0 100.12.242.176
0 100.1.227.10
0 100.12.46.230
0 100.12.50.64
0 100.12.71.66
0 100.1.38.139
Removing everything from that file and adding one line at a time fixed the issue. I'm not sure why probably something odd caused by copy and pasting from an Excel document is my best guess.
:~$ grep -w "100\.4\.144\.28" ips* | wc -l
11
~$ grep -wf match/strings.txt ips* | wc -l
11
$ grep -wf match/strings.txt ips* | wc -l
29
$ grep -w "107\.195\.45\.250" ips* | wc -l
18
$ grep -wf match/strings.txt ips* | wc -l
37
$ grep -w "172\.58\.23\.19" ips* | wc -l
8
Upvotes: 0
Views: 334
Reputation: 60438
Are you certain that your files contain what you think they do? I just gave it a go. What is different about what I have and what you have?
$ 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
Upvotes: 1