A grep code is not yielding the results it used to yield before

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
A small grep code I used to use very often is not working as before. Bellow there is an example in which I have a data set with 7 rows and 3 columns, the dataset is named animal.txt:
Animal Habitat Family
Bear forest Ursidae
Dog house Canidae
Cat house Fenidae
Wolf mountain Canidae
Eagle mountain Accipitridae
Lion sabana Fenidae
I have a list with the names of 3 animals. What I want is to extract the lines that contains those names of animals. The list is named animal3.txt.
Dog
Cat
Bear
Both the dataset and the list are tab delimited files. The code I am using is:
grep -w -F -f ./animal3.txt ./animal.txt > ./output.txt
The output only have the line of Bear I have searched in several forums and haven't find something similar. I really don't know what is going on or what I am doing wrong.
Thanks and sorry for this very basic question.
grep regular-expression
 |Â
show 1 more comment
up vote
0
down vote
favorite
A small grep code I used to use very often is not working as before. Bellow there is an example in which I have a data set with 7 rows and 3 columns, the dataset is named animal.txt:
Animal Habitat Family
Bear forest Ursidae
Dog house Canidae
Cat house Fenidae
Wolf mountain Canidae
Eagle mountain Accipitridae
Lion sabana Fenidae
I have a list with the names of 3 animals. What I want is to extract the lines that contains those names of animals. The list is named animal3.txt.
Dog
Cat
Bear
Both the dataset and the list are tab delimited files. The code I am using is:
grep -w -F -f ./animal3.txt ./animal.txt > ./output.txt
The output only have the line of Bear I have searched in several forums and haven't find something similar. I really don't know what is going on or what I am doing wrong.
Thanks and sorry for this very basic question.
grep regular-expression
Works fine for me. However, I'm assuminganimal.hmp.txtis a typo, since you earlier stated the filename wasanimal.txt? Also, all three./are unnecessary. This means the file in the current directory, which is already implicit.
â Sparhawk
Mar 17 at 0:42
2
The first thing I would check for is non-printing characters in the file(s) - in particular, DOS line endings - e.g.cat -et animal3.txt
â steeldriver
Mar 17 at 0:42
1
Is it possible that your file animal3.txt does not have the style of line ending it used to have, ie now it's CRLF?
â Ulrich Schwarz
Mar 17 at 0:43
Yes, I already checked it that and all the animal's names have those ^M$ terminators except the last one. So, It is very probably that is the reason grep is not working well. I did't know that. So, how can I remove that?
â Fersal
Mar 17 at 0:59
You can use any of the methods described here: Remove ^M character from log files
â steeldriver
Mar 17 at 1:11
 |Â
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
A small grep code I used to use very often is not working as before. Bellow there is an example in which I have a data set with 7 rows and 3 columns, the dataset is named animal.txt:
Animal Habitat Family
Bear forest Ursidae
Dog house Canidae
Cat house Fenidae
Wolf mountain Canidae
Eagle mountain Accipitridae
Lion sabana Fenidae
I have a list with the names of 3 animals. What I want is to extract the lines that contains those names of animals. The list is named animal3.txt.
Dog
Cat
Bear
Both the dataset and the list are tab delimited files. The code I am using is:
grep -w -F -f ./animal3.txt ./animal.txt > ./output.txt
The output only have the line of Bear I have searched in several forums and haven't find something similar. I really don't know what is going on or what I am doing wrong.
Thanks and sorry for this very basic question.
grep regular-expression
A small grep code I used to use very often is not working as before. Bellow there is an example in which I have a data set with 7 rows and 3 columns, the dataset is named animal.txt:
Animal Habitat Family
Bear forest Ursidae
Dog house Canidae
Cat house Fenidae
Wolf mountain Canidae
Eagle mountain Accipitridae
Lion sabana Fenidae
I have a list with the names of 3 animals. What I want is to extract the lines that contains those names of animals. The list is named animal3.txt.
Dog
Cat
Bear
Both the dataset and the list are tab delimited files. The code I am using is:
grep -w -F -f ./animal3.txt ./animal.txt > ./output.txt
The output only have the line of Bear I have searched in several forums and haven't find something similar. I really don't know what is going on or what I am doing wrong.
Thanks and sorry for this very basic question.
grep regular-expression
edited Mar 17 at 0:45
asked Mar 17 at 0:34
Fersal
424
424
Works fine for me. However, I'm assuminganimal.hmp.txtis a typo, since you earlier stated the filename wasanimal.txt? Also, all three./are unnecessary. This means the file in the current directory, which is already implicit.
â Sparhawk
Mar 17 at 0:42
2
The first thing I would check for is non-printing characters in the file(s) - in particular, DOS line endings - e.g.cat -et animal3.txt
â steeldriver
Mar 17 at 0:42
1
Is it possible that your file animal3.txt does not have the style of line ending it used to have, ie now it's CRLF?
â Ulrich Schwarz
Mar 17 at 0:43
Yes, I already checked it that and all the animal's names have those ^M$ terminators except the last one. So, It is very probably that is the reason grep is not working well. I did't know that. So, how can I remove that?
â Fersal
Mar 17 at 0:59
You can use any of the methods described here: Remove ^M character from log files
â steeldriver
Mar 17 at 1:11
 |Â
show 1 more comment
Works fine for me. However, I'm assuminganimal.hmp.txtis a typo, since you earlier stated the filename wasanimal.txt? Also, all three./are unnecessary. This means the file in the current directory, which is already implicit.
â Sparhawk
Mar 17 at 0:42
2
The first thing I would check for is non-printing characters in the file(s) - in particular, DOS line endings - e.g.cat -et animal3.txt
â steeldriver
Mar 17 at 0:42
1
Is it possible that your file animal3.txt does not have the style of line ending it used to have, ie now it's CRLF?
â Ulrich Schwarz
Mar 17 at 0:43
Yes, I already checked it that and all the animal's names have those ^M$ terminators except the last one. So, It is very probably that is the reason grep is not working well. I did't know that. So, how can I remove that?
â Fersal
Mar 17 at 0:59
You can use any of the methods described here: Remove ^M character from log files
â steeldriver
Mar 17 at 1:11
Works fine for me. However, I'm assuming
animal.hmp.txt is a typo, since you earlier stated the filename was animal.txt? Also, all three ./ are unnecessary. This means the file in the current directory, which is already implicit.â Sparhawk
Mar 17 at 0:42
Works fine for me. However, I'm assuming
animal.hmp.txt is a typo, since you earlier stated the filename was animal.txt? Also, all three ./ are unnecessary. This means the file in the current directory, which is already implicit.â Sparhawk
Mar 17 at 0:42
2
2
The first thing I would check for is non-printing characters in the file(s) - in particular, DOS line endings - e.g.
cat -et animal3.txtâ steeldriver
Mar 17 at 0:42
The first thing I would check for is non-printing characters in the file(s) - in particular, DOS line endings - e.g.
cat -et animal3.txtâ steeldriver
Mar 17 at 0:42
1
1
Is it possible that your file animal3.txt does not have the style of line ending it used to have, ie now it's CRLF?
â Ulrich Schwarz
Mar 17 at 0:43
Is it possible that your file animal3.txt does not have the style of line ending it used to have, ie now it's CRLF?
â Ulrich Schwarz
Mar 17 at 0:43
Yes, I already checked it that and all the animal's names have those ^M$ terminators except the last one. So, It is very probably that is the reason grep is not working well. I did't know that. So, how can I remove that?
â Fersal
Mar 17 at 0:59
Yes, I already checked it that and all the animal's names have those ^M$ terminators except the last one. So, It is very probably that is the reason grep is not working well. I did't know that. So, how can I remove that?
â Fersal
Mar 17 at 0:59
You can use any of the methods described here: Remove ^M character from log files
â steeldriver
Mar 17 at 1:11
You can use any of the methods described here: Remove ^M character from log files
â steeldriver
Mar 17 at 1:11
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
From comments I gather that the animal3.txt file has carriage returns at the end of at least some lines. These characters becomes part of the pattern that grep is using which in turn makes the patterns not match in the animal.txt file.
If the file is not supposed to have any carriage returns, then you may use
tr -d 'r' <animal3.txt >animal3-new.txt
to delete them. You may then replace the original animal3.txt file with the corrected animal3-new.txt file.
The OP mentions the last line has no ^M which would indicate it's a MS-DOS type file also missing the last line delimiter. dos2unix would fix all those problems. Those types of files could also have UTF-8 BOMs or other idiosyncrasies.
â Stéphane Chazelas
Mar 17 at 8:17
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
From comments I gather that the animal3.txt file has carriage returns at the end of at least some lines. These characters becomes part of the pattern that grep is using which in turn makes the patterns not match in the animal.txt file.
If the file is not supposed to have any carriage returns, then you may use
tr -d 'r' <animal3.txt >animal3-new.txt
to delete them. You may then replace the original animal3.txt file with the corrected animal3-new.txt file.
The OP mentions the last line has no ^M which would indicate it's a MS-DOS type file also missing the last line delimiter. dos2unix would fix all those problems. Those types of files could also have UTF-8 BOMs or other idiosyncrasies.
â Stéphane Chazelas
Mar 17 at 8:17
add a comment |Â
up vote
1
down vote
From comments I gather that the animal3.txt file has carriage returns at the end of at least some lines. These characters becomes part of the pattern that grep is using which in turn makes the patterns not match in the animal.txt file.
If the file is not supposed to have any carriage returns, then you may use
tr -d 'r' <animal3.txt >animal3-new.txt
to delete them. You may then replace the original animal3.txt file with the corrected animal3-new.txt file.
The OP mentions the last line has no ^M which would indicate it's a MS-DOS type file also missing the last line delimiter. dos2unix would fix all those problems. Those types of files could also have UTF-8 BOMs or other idiosyncrasies.
â Stéphane Chazelas
Mar 17 at 8:17
add a comment |Â
up vote
1
down vote
up vote
1
down vote
From comments I gather that the animal3.txt file has carriage returns at the end of at least some lines. These characters becomes part of the pattern that grep is using which in turn makes the patterns not match in the animal.txt file.
If the file is not supposed to have any carriage returns, then you may use
tr -d 'r' <animal3.txt >animal3-new.txt
to delete them. You may then replace the original animal3.txt file with the corrected animal3-new.txt file.
From comments I gather that the animal3.txt file has carriage returns at the end of at least some lines. These characters becomes part of the pattern that grep is using which in turn makes the patterns not match in the animal.txt file.
If the file is not supposed to have any carriage returns, then you may use
tr -d 'r' <animal3.txt >animal3-new.txt
to delete them. You may then replace the original animal3.txt file with the corrected animal3-new.txt file.
answered Mar 17 at 8:11
Kusalananda
103k13201317
103k13201317
The OP mentions the last line has no ^M which would indicate it's a MS-DOS type file also missing the last line delimiter. dos2unix would fix all those problems. Those types of files could also have UTF-8 BOMs or other idiosyncrasies.
â Stéphane Chazelas
Mar 17 at 8:17
add a comment |Â
The OP mentions the last line has no ^M which would indicate it's a MS-DOS type file also missing the last line delimiter. dos2unix would fix all those problems. Those types of files could also have UTF-8 BOMs or other idiosyncrasies.
â Stéphane Chazelas
Mar 17 at 8:17
The OP mentions the last line has no ^M which would indicate it's a MS-DOS type file also missing the last line delimiter. dos2unix would fix all those problems. Those types of files could also have UTF-8 BOMs or other idiosyncrasies.
â Stéphane Chazelas
Mar 17 at 8:17
The OP mentions the last line has no ^M which would indicate it's a MS-DOS type file also missing the last line delimiter. dos2unix would fix all those problems. Those types of files could also have UTF-8 BOMs or other idiosyncrasies.
â Stéphane Chazelas
Mar 17 at 8:17
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%2f430712%2fa-grep-code-is-not-yielding-the-results-it-used-to-yield-before%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
Works fine for me. However, I'm assuming
animal.hmp.txtis a typo, since you earlier stated the filename wasanimal.txt? Also, all three./are unnecessary. This means the file in the current directory, which is already implicit.â Sparhawk
Mar 17 at 0:42
2
The first thing I would check for is non-printing characters in the file(s) - in particular, DOS line endings - e.g.
cat -et animal3.txtâ steeldriver
Mar 17 at 0:42
1
Is it possible that your file animal3.txt does not have the style of line ending it used to have, ie now it's CRLF?
â Ulrich Schwarz
Mar 17 at 0:43
Yes, I already checked it that and all the animal's names have those ^M$ terminators except the last one. So, It is very probably that is the reason grep is not working well. I did't know that. So, how can I remove that?
â Fersal
Mar 17 at 0:59
You can use any of the methods described here: Remove ^M character from log files
â steeldriver
Mar 17 at 1:11