Check if a file contains an exact match string from another file using grep

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have two files : file1 and file2. The content of both files is the following



  • file1: Hello

  • file2: Bla bla bla bla Hello Bla Bla bla bla bla

My objective is to see if I can find an exact match of Hello in file2.




I used the following command: grep -F -f file1 file2. As output I get Bla bla bla bla Hello Bla Bla bla bla bla.



But suppose I change Hello in file1 to just "H" and I run the grep command again
; I also get as output Bla bla bla bla Hello Bla Bla bla bla bla.



What can I do in order to solve that problem and search only for an exact match?







share|improve this question

















  • 2




    You are getting an exact match. If you want to match a complete word, use -w with grep.
    – Kusalananda
    Jul 4 at 14:02














up vote
1
down vote

favorite












I have two files : file1 and file2. The content of both files is the following



  • file1: Hello

  • file2: Bla bla bla bla Hello Bla Bla bla bla bla

My objective is to see if I can find an exact match of Hello in file2.




I used the following command: grep -F -f file1 file2. As output I get Bla bla bla bla Hello Bla Bla bla bla bla.



But suppose I change Hello in file1 to just "H" and I run the grep command again
; I also get as output Bla bla bla bla Hello Bla Bla bla bla bla.



What can I do in order to solve that problem and search only for an exact match?







share|improve this question

















  • 2




    You are getting an exact match. If you want to match a complete word, use -w with grep.
    – Kusalananda
    Jul 4 at 14:02












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have two files : file1 and file2. The content of both files is the following



  • file1: Hello

  • file2: Bla bla bla bla Hello Bla Bla bla bla bla

My objective is to see if I can find an exact match of Hello in file2.




I used the following command: grep -F -f file1 file2. As output I get Bla bla bla bla Hello Bla Bla bla bla bla.



But suppose I change Hello in file1 to just "H" and I run the grep command again
; I also get as output Bla bla bla bla Hello Bla Bla bla bla bla.



What can I do in order to solve that problem and search only for an exact match?







share|improve this question













I have two files : file1 and file2. The content of both files is the following



  • file1: Hello

  • file2: Bla bla bla bla Hello Bla Bla bla bla bla

My objective is to see if I can find an exact match of Hello in file2.




I used the following command: grep -F -f file1 file2. As output I get Bla bla bla bla Hello Bla Bla bla bla bla.



But suppose I change Hello in file1 to just "H" and I run the grep command again
; I also get as output Bla bla bla bla Hello Bla Bla bla bla bla.



What can I do in order to solve that problem and search only for an exact match?









share|improve this question












share|improve this question




share|improve this question








edited Jul 5 at 0:05









Jeff Schaller

30.8k846104




30.8k846104









asked Jul 4 at 13:55









Hani Gotc

554




554







  • 2




    You are getting an exact match. If you want to match a complete word, use -w with grep.
    – Kusalananda
    Jul 4 at 14:02












  • 2




    You are getting an exact match. If you want to match a complete word, use -w with grep.
    – Kusalananda
    Jul 4 at 14:02







2




2




You are getting an exact match. If you want to match a complete word, use -w with grep.
– Kusalananda
Jul 4 at 14:02




You are getting an exact match. If you want to match a complete word, use -w with grep.
– Kusalananda
Jul 4 at 14:02










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You can add those two arguments:




  • -w Matches only word/words instead of substring


  • -o Display only matched pattern instead of whole line

So command will be:



grep -ow -F -f file1 file2


First exemple will output:



Hello


Second one won't output anything since there is no exact match found.






share|improve this answer























  • The second example would not output anything as there is no H word in the data.
    – Kusalananda
    Jul 4 at 14:11










  • You're right. Corrected. Thanks
    – Kevin Lemaire
    Jul 4 at 14:11











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453440%2fcheck-if-a-file-contains-an-exact-match-string-from-another-file-using-grep%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You can add those two arguments:




  • -w Matches only word/words instead of substring


  • -o Display only matched pattern instead of whole line

So command will be:



grep -ow -F -f file1 file2


First exemple will output:



Hello


Second one won't output anything since there is no exact match found.






share|improve this answer























  • The second example would not output anything as there is no H word in the data.
    – Kusalananda
    Jul 4 at 14:11










  • You're right. Corrected. Thanks
    – Kevin Lemaire
    Jul 4 at 14:11















up vote
2
down vote



accepted










You can add those two arguments:




  • -w Matches only word/words instead of substring


  • -o Display only matched pattern instead of whole line

So command will be:



grep -ow -F -f file1 file2


First exemple will output:



Hello


Second one won't output anything since there is no exact match found.






share|improve this answer























  • The second example would not output anything as there is no H word in the data.
    – Kusalananda
    Jul 4 at 14:11










  • You're right. Corrected. Thanks
    – Kevin Lemaire
    Jul 4 at 14:11













up vote
2
down vote



accepted







up vote
2
down vote



accepted






You can add those two arguments:




  • -w Matches only word/words instead of substring


  • -o Display only matched pattern instead of whole line

So command will be:



grep -ow -F -f file1 file2


First exemple will output:



Hello


Second one won't output anything since there is no exact match found.






share|improve this answer















You can add those two arguments:




  • -w Matches only word/words instead of substring


  • -o Display only matched pattern instead of whole line

So command will be:



grep -ow -F -f file1 file2


First exemple will output:



Hello


Second one won't output anything since there is no exact match found.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 4 at 14:12


























answered Jul 4 at 14:02









Kevin Lemaire

1,039421




1,039421











  • The second example would not output anything as there is no H word in the data.
    – Kusalananda
    Jul 4 at 14:11










  • You're right. Corrected. Thanks
    – Kevin Lemaire
    Jul 4 at 14:11

















  • The second example would not output anything as there is no H word in the data.
    – Kusalananda
    Jul 4 at 14:11










  • You're right. Corrected. Thanks
    – Kevin Lemaire
    Jul 4 at 14:11
















The second example would not output anything as there is no H word in the data.
– Kusalananda
Jul 4 at 14:11




The second example would not output anything as there is no H word in the data.
– Kusalananda
Jul 4 at 14:11












You're right. Corrected. Thanks
– Kevin Lemaire
Jul 4 at 14:11





You're right. Corrected. Thanks
– Kevin Lemaire
Jul 4 at 14:11













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453440%2fcheck-if-a-file-contains-an-exact-match-string-from-another-file-using-grep%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?