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

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question






















  • 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




    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














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.







share|improve this question






















  • 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




    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












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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








edited Mar 17 at 0:45

























asked Mar 17 at 0:34









Fersal

424




424











  • 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




    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






  • 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










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.






share|improve this answer




















  • 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










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%2f430712%2fa-grep-code-is-not-yielding-the-results-it-used-to-yield-before%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
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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)