Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

bash - grep -Ff producing invalid output

I'm using

code -

 grep -Ff list.txt C:/data/*.txt > found.txt

but it keeps outputting invalid responses, lines don't contain the emails i input..

list.txt contains -

email@email.com
customer@email.com
imadmin@gmail.com
newcustomer@email.com
helloworld@yes.com

and so on.. email to match on each line,

search files contain -

user1:phonenumber1:email@email.com:last-active:recent
user2:phonennumber2:customer@email.com:last-active:inactive
user3:phonenumber3:blablarandom@bla.com:last-active:never

then another may contain -

blublublu         email@email.com         phonenumber         subscribed
nanananana        customer@email.com      phonenumber         unsubscribed
useruser          noemailinput@noemail.com       phonenumber      pending

so what I'm trying to do is present grep with a list of emails/list of strings " list.txt " and to then search the directory provided for matches of each string and output the entire line that contains each match.

example of output in this case would be -

user1:phonenumber1:email@email.com:last-active:recent
user2:phonennumber2:customer@email.com:last-active:inactive
blublublu         email@email.com         phonenumber         subscribed
nanananana        customer@email.com      phonenumber         unsubscribed

yet it wouldn't output the other two lines -

 user3:phonenumber3:blablarandom@bla.com:last-active:never
 useruser          noemailinput@noemail.com       phonenumber      pending

because no string is within that line.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The file list.txt probably contains empty lines or some of the separators. When I added : to list.txt, all the lines from the first sample started to match. Similarly, adding a space made all the lines from the second sample match. Adding @ causes the same symptoms.

Try running grep -oFf ... (if your grep supports -o) to see the exact matching parts. If there are empty lines in list.txt, the number of matches will be less than the number of matches without -o. Try searching the output of -o for extremely short outputs to check for suspicious strings. You can also examine the shortest lines in list.txt.

while read line ; do echo ${#line} "$line" ; done < list.txt | sort -nk1,1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...