I want to find if a word exists in a file
up vote
1
down vote
favorite
I have a directory with some files.
I want to know if a word exists in each file.
$1 is my directory that I give it as a parameter
e.g. ./myscript.sh /mnt/c/User/er/Desktop/shows
I have written this code but the terminal shows this error : [: it21754 :binary operator expected
#!/bin/bash
for a in $( find "$1" -type f )
do
if[ egrep 'it21754' $a ]
then
echo "0"
else
echo "1"
fi
done
files grep find directory for
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
1
down vote
favorite
I have a directory with some files.
I want to know if a word exists in each file.
$1 is my directory that I give it as a parameter
e.g. ./myscript.sh /mnt/c/User/er/Desktop/shows
I have written this code but the terminal shows this error : [: it21754 :binary operator expected
#!/bin/bash
for a in $( find "$1" -type f )
do
if[ egrep 'it21754' $a ]
then
echo "0"
else
echo "1"
fi
done
files grep find directory for
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
It's not entirely clear what behavior you're looking for. Do you really want to output a 0 or 1 for every file in the directory? Once you fix the error and this is successful, you will get a series of 0s and 1s, and it won't be immediately obvious which files contain the word you're looking for. If you could describe your desired output, you'll be more likely to get a helpful answer.
– De Novo
2 days ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a directory with some files.
I want to know if a word exists in each file.
$1 is my directory that I give it as a parameter
e.g. ./myscript.sh /mnt/c/User/er/Desktop/shows
I have written this code but the terminal shows this error : [: it21754 :binary operator expected
#!/bin/bash
for a in $( find "$1" -type f )
do
if[ egrep 'it21754' $a ]
then
echo "0"
else
echo "1"
fi
done
files grep find directory for
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a directory with some files.
I want to know if a word exists in each file.
$1 is my directory that I give it as a parameter
e.g. ./myscript.sh /mnt/c/User/er/Desktop/shows
I have written this code but the terminal shows this error : [: it21754 :binary operator expected
#!/bin/bash
for a in $( find "$1" -type f )
do
if[ egrep 'it21754' $a ]
then
echo "0"
else
echo "1"
fi
done
files grep find directory for
files grep find directory for
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
GAD3R
25k1749106
25k1749106
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
Eleftheria
61
61
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Eleftheria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
It's not entirely clear what behavior you're looking for. Do you really want to output a 0 or 1 for every file in the directory? Once you fix the error and this is successful, you will get a series of 0s and 1s, and it won't be immediately obvious which files contain the word you're looking for. If you could describe your desired output, you'll be more likely to get a helpful answer.
– De Novo
2 days ago
add a comment |
1
It's not entirely clear what behavior you're looking for. Do you really want to output a 0 or 1 for every file in the directory? Once you fix the error and this is successful, you will get a series of 0s and 1s, and it won't be immediately obvious which files contain the word you're looking for. If you could describe your desired output, you'll be more likely to get a helpful answer.
– De Novo
2 days ago
1
1
It's not entirely clear what behavior you're looking for. Do you really want to output a 0 or 1 for every file in the directory? Once you fix the error and this is successful, you will get a series of 0s and 1s, and it won't be immediately obvious which files contain the word you're looking for. If you could describe your desired output, you'll be more likely to get a helpful answer.
– De Novo
2 days ago
It's not entirely clear what behavior you're looking for. Do you really want to output a 0 or 1 for every file in the directory? Once you fix the error and this is successful, you will get a series of 0s and 1s, and it won't be immediately obvious which files contain the word you're looking for. If you could describe your desired output, you'll be more likely to get a helpful answer.
– De Novo
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
Don't loop over the output of find. It's rather inelegant as the complete find command has to finish and produce all its output before the loop can even run its first iteration. Furthermore you disqualify your script from working correctly with filenames that contain spaces. The result of the command substitution would be split on spaces to produce words for the loop to loop over.
Instead, let the find command generate filenames for the loop like this:
find "$1" -type f -exec sh -c '...loop here...' sh {} +
We'll come back to this shortly.
The syntax on the line
if[ egrep 'it21754' $a ]
is a bit wonky, and you definitely do not need egrep here (grep is enough).
By "wonky" I mean that a [ ... ] test usually looks like [ op arg ] or [ arg op arg ] where op is some operator like -f or -eq or = and arg is a string. You use egrep and it21754 and whatever $a expands to, which is what generates the error that you get. You can't run egrep in this context really, and you don't need to.
The correct if-statement is
if grep -qwF 'it21754' "$a"; then
echo 0
else
echo 1
fi
The -qwF flags makes grep quiet (-q), it makes it match using string comparisons rather than regular expressions (-F), and it assures that the string matched will be a complete word rather than a substring (-w). Here we use the exit status of grep to choose whether to output a 0 (for a match) or 1 (for no match).
I've also double quoted $a as "$a" just in case the filename contains any spaces or filename globbing characters.
Interestingly enough, those are the numbers that grep uses for its exit status anyway, so we could shorten the whole thing to just
grep -qwF 'it21754' "$a"
echo "$?"
The $? variable holds the exit status of the most recently executed command.
Plugging that into our find command, we get
find "$1" -type f -exec sh -c '
for a do
grep -qwF "it21754" "$a"
echo "$?"
done' sh {} +
This will produce a number of ones and zeroes depending on whether the files contain the given word or not. However, I think you may be looking for the filenames of the files that contain the word rather than a zero or a one. For that we may use
find "$1" -type f -exec grep -lwF 'it21754' {} +
Here, I've changed the -q to a -l ("dash ell"). This calls grep with the found files and grep will only output the filenames of the ones that contains the given word. We don't need the sh -c bit any longer since we're calling a single utility (not grep and echo).
Of course, you could do the same thing with just grep:
grep -R -lwF 'it21754' "$1"
With -R, grep will search the subdirectories given on the command line recursively.
It may be worth noting that the -w and -R flags of grep are not standard, but implemented by the most common grep variants.
Related:
- Understanding the -exec option of `find`
re:grepis enough, it looks like the OP is actually looking for a literal match, rather than a pattern match, so evenfgrepis enough.
– De Novo
2 days ago
@DeNovo This is what the last command does.grep -Fis equivalent tofgrep.
– Kusalananda
2 days ago
Ahh yes, I see you used the -F flag now. I had just skimmed to see if there was any need for me to hit submit on the answer I was working on. You may want to clarify that by "the[ egrep 'it21754' $a ]syntax is wonky", you mean that is what is throwing the error. There's a lot of text in this (excellent) answer. It may be helpful to call attention to the command that is throwing the error.
– De Novo
2 days ago
1
@DeNovo Thanks. I will expand on the "wonky" bit.
– Kusalananda
2 days ago
add a comment |
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: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Eleftheria is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487428%2fi-want-to-find-if-a-word-exists-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Don't loop over the output of find. It's rather inelegant as the complete find command has to finish and produce all its output before the loop can even run its first iteration. Furthermore you disqualify your script from working correctly with filenames that contain spaces. The result of the command substitution would be split on spaces to produce words for the loop to loop over.
Instead, let the find command generate filenames for the loop like this:
find "$1" -type f -exec sh -c '...loop here...' sh {} +
We'll come back to this shortly.
The syntax on the line
if[ egrep 'it21754' $a ]
is a bit wonky, and you definitely do not need egrep here (grep is enough).
By "wonky" I mean that a [ ... ] test usually looks like [ op arg ] or [ arg op arg ] where op is some operator like -f or -eq or = and arg is a string. You use egrep and it21754 and whatever $a expands to, which is what generates the error that you get. You can't run egrep in this context really, and you don't need to.
The correct if-statement is
if grep -qwF 'it21754' "$a"; then
echo 0
else
echo 1
fi
The -qwF flags makes grep quiet (-q), it makes it match using string comparisons rather than regular expressions (-F), and it assures that the string matched will be a complete word rather than a substring (-w). Here we use the exit status of grep to choose whether to output a 0 (for a match) or 1 (for no match).
I've also double quoted $a as "$a" just in case the filename contains any spaces or filename globbing characters.
Interestingly enough, those are the numbers that grep uses for its exit status anyway, so we could shorten the whole thing to just
grep -qwF 'it21754' "$a"
echo "$?"
The $? variable holds the exit status of the most recently executed command.
Plugging that into our find command, we get
find "$1" -type f -exec sh -c '
for a do
grep -qwF "it21754" "$a"
echo "$?"
done' sh {} +
This will produce a number of ones and zeroes depending on whether the files contain the given word or not. However, I think you may be looking for the filenames of the files that contain the word rather than a zero or a one. For that we may use
find "$1" -type f -exec grep -lwF 'it21754' {} +
Here, I've changed the -q to a -l ("dash ell"). This calls grep with the found files and grep will only output the filenames of the ones that contains the given word. We don't need the sh -c bit any longer since we're calling a single utility (not grep and echo).
Of course, you could do the same thing with just grep:
grep -R -lwF 'it21754' "$1"
With -R, grep will search the subdirectories given on the command line recursively.
It may be worth noting that the -w and -R flags of grep are not standard, but implemented by the most common grep variants.
Related:
- Understanding the -exec option of `find`
re:grepis enough, it looks like the OP is actually looking for a literal match, rather than a pattern match, so evenfgrepis enough.
– De Novo
2 days ago
@DeNovo This is what the last command does.grep -Fis equivalent tofgrep.
– Kusalananda
2 days ago
Ahh yes, I see you used the -F flag now. I had just skimmed to see if there was any need for me to hit submit on the answer I was working on. You may want to clarify that by "the[ egrep 'it21754' $a ]syntax is wonky", you mean that is what is throwing the error. There's a lot of text in this (excellent) answer. It may be helpful to call attention to the command that is throwing the error.
– De Novo
2 days ago
1
@DeNovo Thanks. I will expand on the "wonky" bit.
– Kusalananda
2 days ago
add a comment |
up vote
3
down vote
Don't loop over the output of find. It's rather inelegant as the complete find command has to finish and produce all its output before the loop can even run its first iteration. Furthermore you disqualify your script from working correctly with filenames that contain spaces. The result of the command substitution would be split on spaces to produce words for the loop to loop over.
Instead, let the find command generate filenames for the loop like this:
find "$1" -type f -exec sh -c '...loop here...' sh {} +
We'll come back to this shortly.
The syntax on the line
if[ egrep 'it21754' $a ]
is a bit wonky, and you definitely do not need egrep here (grep is enough).
By "wonky" I mean that a [ ... ] test usually looks like [ op arg ] or [ arg op arg ] where op is some operator like -f or -eq or = and arg is a string. You use egrep and it21754 and whatever $a expands to, which is what generates the error that you get. You can't run egrep in this context really, and you don't need to.
The correct if-statement is
if grep -qwF 'it21754' "$a"; then
echo 0
else
echo 1
fi
The -qwF flags makes grep quiet (-q), it makes it match using string comparisons rather than regular expressions (-F), and it assures that the string matched will be a complete word rather than a substring (-w). Here we use the exit status of grep to choose whether to output a 0 (for a match) or 1 (for no match).
I've also double quoted $a as "$a" just in case the filename contains any spaces or filename globbing characters.
Interestingly enough, those are the numbers that grep uses for its exit status anyway, so we could shorten the whole thing to just
grep -qwF 'it21754' "$a"
echo "$?"
The $? variable holds the exit status of the most recently executed command.
Plugging that into our find command, we get
find "$1" -type f -exec sh -c '
for a do
grep -qwF "it21754" "$a"
echo "$?"
done' sh {} +
This will produce a number of ones and zeroes depending on whether the files contain the given word or not. However, I think you may be looking for the filenames of the files that contain the word rather than a zero or a one. For that we may use
find "$1" -type f -exec grep -lwF 'it21754' {} +
Here, I've changed the -q to a -l ("dash ell"). This calls grep with the found files and grep will only output the filenames of the ones that contains the given word. We don't need the sh -c bit any longer since we're calling a single utility (not grep and echo).
Of course, you could do the same thing with just grep:
grep -R -lwF 'it21754' "$1"
With -R, grep will search the subdirectories given on the command line recursively.
It may be worth noting that the -w and -R flags of grep are not standard, but implemented by the most common grep variants.
Related:
- Understanding the -exec option of `find`
re:grepis enough, it looks like the OP is actually looking for a literal match, rather than a pattern match, so evenfgrepis enough.
– De Novo
2 days ago
@DeNovo This is what the last command does.grep -Fis equivalent tofgrep.
– Kusalananda
2 days ago
Ahh yes, I see you used the -F flag now. I had just skimmed to see if there was any need for me to hit submit on the answer I was working on. You may want to clarify that by "the[ egrep 'it21754' $a ]syntax is wonky", you mean that is what is throwing the error. There's a lot of text in this (excellent) answer. It may be helpful to call attention to the command that is throwing the error.
– De Novo
2 days ago
1
@DeNovo Thanks. I will expand on the "wonky" bit.
– Kusalananda
2 days ago
add a comment |
up vote
3
down vote
up vote
3
down vote
Don't loop over the output of find. It's rather inelegant as the complete find command has to finish and produce all its output before the loop can even run its first iteration. Furthermore you disqualify your script from working correctly with filenames that contain spaces. The result of the command substitution would be split on spaces to produce words for the loop to loop over.
Instead, let the find command generate filenames for the loop like this:
find "$1" -type f -exec sh -c '...loop here...' sh {} +
We'll come back to this shortly.
The syntax on the line
if[ egrep 'it21754' $a ]
is a bit wonky, and you definitely do not need egrep here (grep is enough).
By "wonky" I mean that a [ ... ] test usually looks like [ op arg ] or [ arg op arg ] where op is some operator like -f or -eq or = and arg is a string. You use egrep and it21754 and whatever $a expands to, which is what generates the error that you get. You can't run egrep in this context really, and you don't need to.
The correct if-statement is
if grep -qwF 'it21754' "$a"; then
echo 0
else
echo 1
fi
The -qwF flags makes grep quiet (-q), it makes it match using string comparisons rather than regular expressions (-F), and it assures that the string matched will be a complete word rather than a substring (-w). Here we use the exit status of grep to choose whether to output a 0 (for a match) or 1 (for no match).
I've also double quoted $a as "$a" just in case the filename contains any spaces or filename globbing characters.
Interestingly enough, those are the numbers that grep uses for its exit status anyway, so we could shorten the whole thing to just
grep -qwF 'it21754' "$a"
echo "$?"
The $? variable holds the exit status of the most recently executed command.
Plugging that into our find command, we get
find "$1" -type f -exec sh -c '
for a do
grep -qwF "it21754" "$a"
echo "$?"
done' sh {} +
This will produce a number of ones and zeroes depending on whether the files contain the given word or not. However, I think you may be looking for the filenames of the files that contain the word rather than a zero or a one. For that we may use
find "$1" -type f -exec grep -lwF 'it21754' {} +
Here, I've changed the -q to a -l ("dash ell"). This calls grep with the found files and grep will only output the filenames of the ones that contains the given word. We don't need the sh -c bit any longer since we're calling a single utility (not grep and echo).
Of course, you could do the same thing with just grep:
grep -R -lwF 'it21754' "$1"
With -R, grep will search the subdirectories given on the command line recursively.
It may be worth noting that the -w and -R flags of grep are not standard, but implemented by the most common grep variants.
Related:
- Understanding the -exec option of `find`
Don't loop over the output of find. It's rather inelegant as the complete find command has to finish and produce all its output before the loop can even run its first iteration. Furthermore you disqualify your script from working correctly with filenames that contain spaces. The result of the command substitution would be split on spaces to produce words for the loop to loop over.
Instead, let the find command generate filenames for the loop like this:
find "$1" -type f -exec sh -c '...loop here...' sh {} +
We'll come back to this shortly.
The syntax on the line
if[ egrep 'it21754' $a ]
is a bit wonky, and you definitely do not need egrep here (grep is enough).
By "wonky" I mean that a [ ... ] test usually looks like [ op arg ] or [ arg op arg ] where op is some operator like -f or -eq or = and arg is a string. You use egrep and it21754 and whatever $a expands to, which is what generates the error that you get. You can't run egrep in this context really, and you don't need to.
The correct if-statement is
if grep -qwF 'it21754' "$a"; then
echo 0
else
echo 1
fi
The -qwF flags makes grep quiet (-q), it makes it match using string comparisons rather than regular expressions (-F), and it assures that the string matched will be a complete word rather than a substring (-w). Here we use the exit status of grep to choose whether to output a 0 (for a match) or 1 (for no match).
I've also double quoted $a as "$a" just in case the filename contains any spaces or filename globbing characters.
Interestingly enough, those are the numbers that grep uses for its exit status anyway, so we could shorten the whole thing to just
grep -qwF 'it21754' "$a"
echo "$?"
The $? variable holds the exit status of the most recently executed command.
Plugging that into our find command, we get
find "$1" -type f -exec sh -c '
for a do
grep -qwF "it21754" "$a"
echo "$?"
done' sh {} +
This will produce a number of ones and zeroes depending on whether the files contain the given word or not. However, I think you may be looking for the filenames of the files that contain the word rather than a zero or a one. For that we may use
find "$1" -type f -exec grep -lwF 'it21754' {} +
Here, I've changed the -q to a -l ("dash ell"). This calls grep with the found files and grep will only output the filenames of the ones that contains the given word. We don't need the sh -c bit any longer since we're calling a single utility (not grep and echo).
Of course, you could do the same thing with just grep:
grep -R -lwF 'it21754' "$1"
With -R, grep will search the subdirectories given on the command line recursively.
It may be worth noting that the -w and -R flags of grep are not standard, but implemented by the most common grep variants.
Related:
- Understanding the -exec option of `find`
edited 2 days ago
answered 2 days ago
Kusalananda
120k16225367
120k16225367
re:grepis enough, it looks like the OP is actually looking for a literal match, rather than a pattern match, so evenfgrepis enough.
– De Novo
2 days ago
@DeNovo This is what the last command does.grep -Fis equivalent tofgrep.
– Kusalananda
2 days ago
Ahh yes, I see you used the -F flag now. I had just skimmed to see if there was any need for me to hit submit on the answer I was working on. You may want to clarify that by "the[ egrep 'it21754' $a ]syntax is wonky", you mean that is what is throwing the error. There's a lot of text in this (excellent) answer. It may be helpful to call attention to the command that is throwing the error.
– De Novo
2 days ago
1
@DeNovo Thanks. I will expand on the "wonky" bit.
– Kusalananda
2 days ago
add a comment |
re:grepis enough, it looks like the OP is actually looking for a literal match, rather than a pattern match, so evenfgrepis enough.
– De Novo
2 days ago
@DeNovo This is what the last command does.grep -Fis equivalent tofgrep.
– Kusalananda
2 days ago
Ahh yes, I see you used the -F flag now. I had just skimmed to see if there was any need for me to hit submit on the answer I was working on. You may want to clarify that by "the[ egrep 'it21754' $a ]syntax is wonky", you mean that is what is throwing the error. There's a lot of text in this (excellent) answer. It may be helpful to call attention to the command that is throwing the error.
– De Novo
2 days ago
1
@DeNovo Thanks. I will expand on the "wonky" bit.
– Kusalananda
2 days ago
re:
grep is enough, it looks like the OP is actually looking for a literal match, rather than a pattern match, so even fgrep is enough.– De Novo
2 days ago
re:
grep is enough, it looks like the OP is actually looking for a literal match, rather than a pattern match, so even fgrep is enough.– De Novo
2 days ago
@DeNovo This is what the last command does.
grep -F is equivalent to fgrep.– Kusalananda
2 days ago
@DeNovo This is what the last command does.
grep -F is equivalent to fgrep.– Kusalananda
2 days ago
Ahh yes, I see you used the -F flag now. I had just skimmed to see if there was any need for me to hit submit on the answer I was working on. You may want to clarify that by "the
[ egrep 'it21754' $a ] syntax is wonky", you mean that is what is throwing the error. There's a lot of text in this (excellent) answer. It may be helpful to call attention to the command that is throwing the error.– De Novo
2 days ago
Ahh yes, I see you used the -F flag now. I had just skimmed to see if there was any need for me to hit submit on the answer I was working on. You may want to clarify that by "the
[ egrep 'it21754' $a ] syntax is wonky", you mean that is what is throwing the error. There's a lot of text in this (excellent) answer. It may be helpful to call attention to the command that is throwing the error.– De Novo
2 days ago
1
1
@DeNovo Thanks. I will expand on the "wonky" bit.
– Kusalananda
2 days ago
@DeNovo Thanks. I will expand on the "wonky" bit.
– Kusalananda
2 days ago
add a comment |
Eleftheria is a new contributor. Be nice, and check out our Code of Conduct.
Eleftheria is a new contributor. Be nice, and check out our Code of Conduct.
Eleftheria is a new contributor. Be nice, and check out our Code of Conduct.
Eleftheria is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487428%2fi-want-to-find-if-a-word-exists-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It's not entirely clear what behavior you're looking for. Do you really want to output a 0 or 1 for every file in the directory? Once you fix the error and this is successful, you will get a series of 0s and 1s, and it won't be immediately obvious which files contain the word you're looking for. If you could describe your desired output, you'll be more likely to get a helpful answer.
– De Novo
2 days ago