Check if a file contains an exact match string from another file using grep
Clash 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?
grep
add a comment |Â
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?
grep
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
add a comment |Â
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?
grep
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?
grep
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
add a comment |Â
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
add a comment |Â
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.
The second example would not output anything as there is noH
word in the data.
â Kusalananda
Jul 4 at 14:11
You're right. Corrected. Thanks
â Kevin Lemaire
Jul 4 at 14:11
add a comment |Â
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.
The second example would not output anything as there is noH
word in the data.
â Kusalananda
Jul 4 at 14:11
You're right. Corrected. Thanks
â Kevin Lemaire
Jul 4 at 14:11
add a comment |Â
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.
The second example would not output anything as there is noH
word in the data.
â Kusalananda
Jul 4 at 14:11
You're right. Corrected. Thanks
â Kevin Lemaire
Jul 4 at 14:11
add a comment |Â
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.
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.
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 noH
word in the data.
â Kusalananda
Jul 4 at 14:11
You're right. Corrected. Thanks
â Kevin Lemaire
Jul 4 at 14:11
add a comment |Â
The second example would not output anything as there is noH
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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