grep how to suppress display of non-matched file?How to “less” a file named “-”?How to show lines after each grep match until other specific match?List the files containing a particular word in their textHow to exclude some files not matching certain extensions with grep?grep string where next line does not contain stringStop grep after Nth files matchedSearch word structure using grepTail Grep - Print surrounding lines until pattern is matchedgrep: Which *patterns* are matched, not which text?How to split an output to two files with grep?How do I grep many files for many strings?
Are there any examples of a variable being normally distributed that is *not* due to the Central Limit Theorem?
Can we compute the area of a quadrilateral with one right angle when we only know the lengths of any three sides?
Personal Teleportation: From Rags to Riches
Is it logically or scientifically possible to artificially send energy to the body?
Little known, relatively unlikely, but scientifically plausible, apocalyptic (or near apocalyptic) events
Do UK voters know if their MP will be the Speaker of the House?
Should I tell management that I intend to leave due to bad software development practices?
Apex Framework / library for consuming REST services
Could the museum Saturn V's be refitted for one more flight?
What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?
Why are the 737's rear doors unusable in a water landing?
How did the Super Star Destroyer Executor get destroyed exactly?
How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)
Plagiarism or not?
What are some good books on Machine Learning and AI like Krugman, Wells and Graddy's "Essentials of Economics"
Assassin's bullet with mercury
Reverse dictionary where values are lists
What mechanic is there to disable a threat instead of killing it?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
What's the in-universe reasoning behind sorcerers needing material components?
iPad being using in wall mount battery swollen
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
How can saying a song's name be a copyright violation?
Im going to France and my passport expires June 19th
grep how to suppress display of non-matched file?
How to “less” a file named “-”?How to show lines after each grep match until other specific match?List the files containing a particular word in their textHow to exclude some files not matching certain extensions with grep?grep string where next line does not contain stringStop grep after Nth files matchedSearch word structure using grepTail Grep - Print surrounding lines until pattern is matchedgrep: Which *patterns* are matched, not which text?How to split an output to two files with grep?How do I grep many files for many strings?
I am trying to find files containing a specific word using grep
. There are many files in the directory (> 500)
Command I run
$ grep 'delete' *
Output
validate_data_stage1:0
validate_data_stage2:0
validate_data_stage3:0
validate_data_stage4:0
validate_data_stage5:0
validate_input_stage1:0
validate_input_stage2:0
validate_input_stage3:0
validate_input_stage4:0
.... and hundred of such lines
These are the files that don't contain the given match. I want to suppress those lines from displaying to stdout. I know of -q
switch, but that would suppress the complete output, which I don't want.
How do I do that?
grep search stdout command-switch
add a comment |
I am trying to find files containing a specific word using grep
. There are many files in the directory (> 500)
Command I run
$ grep 'delete' *
Output
validate_data_stage1:0
validate_data_stage2:0
validate_data_stage3:0
validate_data_stage4:0
validate_data_stage5:0
validate_input_stage1:0
validate_input_stage2:0
validate_input_stage3:0
validate_input_stage4:0
.... and hundred of such lines
These are the files that don't contain the given match. I want to suppress those lines from displaying to stdout. I know of -q
switch, but that would suppress the complete output, which I don't want.
How do I do that?
grep search stdout command-switch
2
Normally,grep
should not print out file names of non-matching files. Actually, it looks likegrep
considers a line with the content0
to be matching. Can you post the exact search pattern you are using?
– Adrian Heine
Nov 22 '12 at 9:31
The complete grep cmd I used wasgrep 'delete' * -R
, but I don't think-R
is causing any issue. And yes, it normally doesn't print the non-matching ones, but not sure what's the case here...
– mtk
Nov 22 '12 at 9:40
@step @everyone.. Strange... After seeing the edited post above. I triedgrep -- 'delete' *
(added--
) and it worked as expected. Removing the--
is leading to the above display.
– mtk
Nov 22 '12 at 11:50
1
@Stephane Indeed you are correct. I searched the dir... and there was a file with name-ci
. Thanks for resolving the issue. You can post this scenario as an answer.
– mtk
Nov 22 '12 at 13:29
add a comment |
I am trying to find files containing a specific word using grep
. There are many files in the directory (> 500)
Command I run
$ grep 'delete' *
Output
validate_data_stage1:0
validate_data_stage2:0
validate_data_stage3:0
validate_data_stage4:0
validate_data_stage5:0
validate_input_stage1:0
validate_input_stage2:0
validate_input_stage3:0
validate_input_stage4:0
.... and hundred of such lines
These are the files that don't contain the given match. I want to suppress those lines from displaying to stdout. I know of -q
switch, but that would suppress the complete output, which I don't want.
How do I do that?
grep search stdout command-switch
I am trying to find files containing a specific word using grep
. There are many files in the directory (> 500)
Command I run
$ grep 'delete' *
Output
validate_data_stage1:0
validate_data_stage2:0
validate_data_stage3:0
validate_data_stage4:0
validate_data_stage5:0
validate_input_stage1:0
validate_input_stage2:0
validate_input_stage3:0
validate_input_stage4:0
.... and hundred of such lines
These are the files that don't contain the given match. I want to suppress those lines from displaying to stdout. I know of -q
switch, but that would suppress the complete output, which I don't want.
How do I do that?
grep search stdout command-switch
grep search stdout command-switch
edited Dec 6 '12 at 13:55
mtk
asked Nov 22 '12 at 8:57
mtkmtk
8,5582864105
8,5582864105
2
Normally,grep
should not print out file names of non-matching files. Actually, it looks likegrep
considers a line with the content0
to be matching. Can you post the exact search pattern you are using?
– Adrian Heine
Nov 22 '12 at 9:31
The complete grep cmd I used wasgrep 'delete' * -R
, but I don't think-R
is causing any issue. And yes, it normally doesn't print the non-matching ones, but not sure what's the case here...
– mtk
Nov 22 '12 at 9:40
@step @everyone.. Strange... After seeing the edited post above. I triedgrep -- 'delete' *
(added--
) and it worked as expected. Removing the--
is leading to the above display.
– mtk
Nov 22 '12 at 11:50
1
@Stephane Indeed you are correct. I searched the dir... and there was a file with name-ci
. Thanks for resolving the issue. You can post this scenario as an answer.
– mtk
Nov 22 '12 at 13:29
add a comment |
2
Normally,grep
should not print out file names of non-matching files. Actually, it looks likegrep
considers a line with the content0
to be matching. Can you post the exact search pattern you are using?
– Adrian Heine
Nov 22 '12 at 9:31
The complete grep cmd I used wasgrep 'delete' * -R
, but I don't think-R
is causing any issue. And yes, it normally doesn't print the non-matching ones, but not sure what's the case here...
– mtk
Nov 22 '12 at 9:40
@step @everyone.. Strange... After seeing the edited post above. I triedgrep -- 'delete' *
(added--
) and it worked as expected. Removing the--
is leading to the above display.
– mtk
Nov 22 '12 at 11:50
1
@Stephane Indeed you are correct. I searched the dir... and there was a file with name-ci
. Thanks for resolving the issue. You can post this scenario as an answer.
– mtk
Nov 22 '12 at 13:29
2
2
Normally,
grep
should not print out file names of non-matching files. Actually, it looks like grep
considers a line with the content 0
to be matching. Can you post the exact search pattern you are using?– Adrian Heine
Nov 22 '12 at 9:31
Normally,
grep
should not print out file names of non-matching files. Actually, it looks like grep
considers a line with the content 0
to be matching. Can you post the exact search pattern you are using?– Adrian Heine
Nov 22 '12 at 9:31
The complete grep cmd I used was
grep 'delete' * -R
, but I don't think -R
is causing any issue. And yes, it normally doesn't print the non-matching ones, but not sure what's the case here...– mtk
Nov 22 '12 at 9:40
The complete grep cmd I used was
grep 'delete' * -R
, but I don't think -R
is causing any issue. And yes, it normally doesn't print the non-matching ones, but not sure what's the case here...– mtk
Nov 22 '12 at 9:40
@step @everyone.. Strange... After seeing the edited post above. I tried
grep -- 'delete' *
(added --
) and it worked as expected. Removing the --
is leading to the above display.– mtk
Nov 22 '12 at 11:50
@step @everyone.. Strange... After seeing the edited post above. I tried
grep -- 'delete' *
(added --
) and it worked as expected. Removing the --
is leading to the above display.– mtk
Nov 22 '12 at 11:50
1
1
@Stephane Indeed you are correct. I searched the dir... and there was a file with name
-ci
. Thanks for resolving the issue. You can post this scenario as an answer.– mtk
Nov 22 '12 at 13:29
@Stephane Indeed you are correct. I searched the dir... and there was a file with name
-ci
. Thanks for resolving the issue. You can post this scenario as an answer.– mtk
Nov 22 '12 at 13:29
add a comment |
4 Answers
4
active
oldest
votes
That's the behavior exhibited by grep -c
.
Probably you have a file whose name starts with -
and contains a c
character and you're using GNU grep without setting the POSIXLY_CORRECT
environment variable.
Use:
grep -- delete *
or better:
grep delete ./*
--
marks the end of options so that that filename will not be considered as an option (with a POSIX grep, it wouldn't since the non-option delete
argument would have marked the end of options), but it wouldn't address the problem of a file called -
. The grep delete ./*
is more robust but has the drawback of outputting the extra ./
for matching files (though that may be considered a bonus since that helps identify file names that contain newline characters).
add a comment |
Add
| grep -v ':0$'
Not very elegant but I think will do the job.
Adding -l
to your command will give you only matches but it will also suppress printing number of matches for each file.
1
-l
worked. the work-around is also good. Thanks
– mtk
Nov 22 '12 at 11:47
4
the grep line should begrep -v ':0$'
so only those at the end of a line are matched (however, thegrep -l
approach is far better as long as you don't need line counts)
– mreithub
Nov 22 '12 at 12:17
add a comment |
Using grep -l
you will only get the files that contain at least one match.
Do you need information about how many matches there are in a file at all? Because the you could skip using -c
which is used to count the number of matches in a file.
edit: And like warlock said using -I
to suppress matches in binary files could also be a good idea.
add a comment |
How to grep nonzero counts:
grep -rIcH 'string' . | grep -v ':0$'
-r
Recurse subdirectories.-I
Ignore binary files (thanks @tongpu, warlock).-c
Show count of matches. Annoyingly, includes 0-count files.-H
Show file name, even if only one file.'string'
your string goes here..
Start from the current directory.| -v ':0$'
Remove 0-count files. (thanks @LaurentiuRoescu)
(I realize the OP was unintentionally -c counting, but google(grep nonzero count) lands here.)
New contributor
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',
autoActivateHeartbeat: false,
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
);
);
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%2f56356%2fgrep-how-to-suppress-display-of-non-matched-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's the behavior exhibited by grep -c
.
Probably you have a file whose name starts with -
and contains a c
character and you're using GNU grep without setting the POSIXLY_CORRECT
environment variable.
Use:
grep -- delete *
or better:
grep delete ./*
--
marks the end of options so that that filename will not be considered as an option (with a POSIX grep, it wouldn't since the non-option delete
argument would have marked the end of options), but it wouldn't address the problem of a file called -
. The grep delete ./*
is more robust but has the drawback of outputting the extra ./
for matching files (though that may be considered a bonus since that helps identify file names that contain newline characters).
add a comment |
That's the behavior exhibited by grep -c
.
Probably you have a file whose name starts with -
and contains a c
character and you're using GNU grep without setting the POSIXLY_CORRECT
environment variable.
Use:
grep -- delete *
or better:
grep delete ./*
--
marks the end of options so that that filename will not be considered as an option (with a POSIX grep, it wouldn't since the non-option delete
argument would have marked the end of options), but it wouldn't address the problem of a file called -
. The grep delete ./*
is more robust but has the drawback of outputting the extra ./
for matching files (though that may be considered a bonus since that helps identify file names that contain newline characters).
add a comment |
That's the behavior exhibited by grep -c
.
Probably you have a file whose name starts with -
and contains a c
character and you're using GNU grep without setting the POSIXLY_CORRECT
environment variable.
Use:
grep -- delete *
or better:
grep delete ./*
--
marks the end of options so that that filename will not be considered as an option (with a POSIX grep, it wouldn't since the non-option delete
argument would have marked the end of options), but it wouldn't address the problem of a file called -
. The grep delete ./*
is more robust but has the drawback of outputting the extra ./
for matching files (though that may be considered a bonus since that helps identify file names that contain newline characters).
That's the behavior exhibited by grep -c
.
Probably you have a file whose name starts with -
and contains a c
character and you're using GNU grep without setting the POSIXLY_CORRECT
environment variable.
Use:
grep -- delete *
or better:
grep delete ./*
--
marks the end of options so that that filename will not be considered as an option (with a POSIX grep, it wouldn't since the non-option delete
argument would have marked the end of options), but it wouldn't address the problem of a file called -
. The grep delete ./*
is more robust but has the drawback of outputting the extra ./
for matching files (though that may be considered a bonus since that helps identify file names that contain newline characters).
edited Nov 23 '12 at 19:59
answered Nov 22 '12 at 13:46
Stéphane ChazelasStéphane Chazelas
313k57592948
313k57592948
add a comment |
add a comment |
Add
| grep -v ':0$'
Not very elegant but I think will do the job.
Adding -l
to your command will give you only matches but it will also suppress printing number of matches for each file.
1
-l
worked. the work-around is also good. Thanks
– mtk
Nov 22 '12 at 11:47
4
the grep line should begrep -v ':0$'
so only those at the end of a line are matched (however, thegrep -l
approach is far better as long as you don't need line counts)
– mreithub
Nov 22 '12 at 12:17
add a comment |
Add
| grep -v ':0$'
Not very elegant but I think will do the job.
Adding -l
to your command will give you only matches but it will also suppress printing number of matches for each file.
1
-l
worked. the work-around is also good. Thanks
– mtk
Nov 22 '12 at 11:47
4
the grep line should begrep -v ':0$'
so only those at the end of a line are matched (however, thegrep -l
approach is far better as long as you don't need line counts)
– mreithub
Nov 22 '12 at 12:17
add a comment |
Add
| grep -v ':0$'
Not very elegant but I think will do the job.
Adding -l
to your command will give you only matches but it will also suppress printing number of matches for each file.
Add
| grep -v ':0$'
Not very elegant but I think will do the job.
Adding -l
to your command will give you only matches but it will also suppress printing number of matches for each file.
edited Jun 27 '17 at 13:57
Stephen Rauch
3,354101529
3,354101529
answered Nov 22 '12 at 9:33
Laurentiu RoescuLaurentiu Roescu
53947
53947
1
-l
worked. the work-around is also good. Thanks
– mtk
Nov 22 '12 at 11:47
4
the grep line should begrep -v ':0$'
so only those at the end of a line are matched (however, thegrep -l
approach is far better as long as you don't need line counts)
– mreithub
Nov 22 '12 at 12:17
add a comment |
1
-l
worked. the work-around is also good. Thanks
– mtk
Nov 22 '12 at 11:47
4
the grep line should begrep -v ':0$'
so only those at the end of a line are matched (however, thegrep -l
approach is far better as long as you don't need line counts)
– mreithub
Nov 22 '12 at 12:17
1
1
-l
worked. the work-around is also good. Thanks– mtk
Nov 22 '12 at 11:47
-l
worked. the work-around is also good. Thanks– mtk
Nov 22 '12 at 11:47
4
4
the grep line should be
grep -v ':0$'
so only those at the end of a line are matched (however, the grep -l
approach is far better as long as you don't need line counts)– mreithub
Nov 22 '12 at 12:17
the grep line should be
grep -v ':0$'
so only those at the end of a line are matched (however, the grep -l
approach is far better as long as you don't need line counts)– mreithub
Nov 22 '12 at 12:17
add a comment |
Using grep -l
you will only get the files that contain at least one match.
Do you need information about how many matches there are in a file at all? Because the you could skip using -c
which is used to count the number of matches in a file.
edit: And like warlock said using -I
to suppress matches in binary files could also be a good idea.
add a comment |
Using grep -l
you will only get the files that contain at least one match.
Do you need information about how many matches there are in a file at all? Because the you could skip using -c
which is used to count the number of matches in a file.
edit: And like warlock said using -I
to suppress matches in binary files could also be a good idea.
add a comment |
Using grep -l
you will only get the files that contain at least one match.
Do you need information about how many matches there are in a file at all? Because the you could skip using -c
which is used to count the number of matches in a file.
edit: And like warlock said using -I
to suppress matches in binary files could also be a good idea.
Using grep -l
you will only get the files that contain at least one match.
Do you need information about how many matches there are in a file at all? Because the you could skip using -c
which is used to count the number of matches in a file.
edit: And like warlock said using -I
to suppress matches in binary files could also be a good idea.
answered Nov 22 '12 at 12:45
tongputongpu
29614
29614
add a comment |
add a comment |
How to grep nonzero counts:
grep -rIcH 'string' . | grep -v ':0$'
-r
Recurse subdirectories.-I
Ignore binary files (thanks @tongpu, warlock).-c
Show count of matches. Annoyingly, includes 0-count files.-H
Show file name, even if only one file.'string'
your string goes here..
Start from the current directory.| -v ':0$'
Remove 0-count files. (thanks @LaurentiuRoescu)
(I realize the OP was unintentionally -c counting, but google(grep nonzero count) lands here.)
New contributor
add a comment |
How to grep nonzero counts:
grep -rIcH 'string' . | grep -v ':0$'
-r
Recurse subdirectories.-I
Ignore binary files (thanks @tongpu, warlock).-c
Show count of matches. Annoyingly, includes 0-count files.-H
Show file name, even if only one file.'string'
your string goes here..
Start from the current directory.| -v ':0$'
Remove 0-count files. (thanks @LaurentiuRoescu)
(I realize the OP was unintentionally -c counting, but google(grep nonzero count) lands here.)
New contributor
add a comment |
How to grep nonzero counts:
grep -rIcH 'string' . | grep -v ':0$'
-r
Recurse subdirectories.-I
Ignore binary files (thanks @tongpu, warlock).-c
Show count of matches. Annoyingly, includes 0-count files.-H
Show file name, even if only one file.'string'
your string goes here..
Start from the current directory.| -v ':0$'
Remove 0-count files. (thanks @LaurentiuRoescu)
(I realize the OP was unintentionally -c counting, but google(grep nonzero count) lands here.)
New contributor
How to grep nonzero counts:
grep -rIcH 'string' . | grep -v ':0$'
-r
Recurse subdirectories.-I
Ignore binary files (thanks @tongpu, warlock).-c
Show count of matches. Annoyingly, includes 0-count files.-H
Show file name, even if only one file.'string'
your string goes here..
Start from the current directory.| -v ':0$'
Remove 0-count files. (thanks @LaurentiuRoescu)
(I realize the OP was unintentionally -c counting, but google(grep nonzero count) lands here.)
New contributor
New contributor
answered 2 days ago
Bob SteinBob Stein
1012
1012
New contributor
New contributor
add a comment |
add a comment |
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.
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%2f56356%2fgrep-how-to-suppress-display-of-non-matched-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
2
Normally,
grep
should not print out file names of non-matching files. Actually, it looks likegrep
considers a line with the content0
to be matching. Can you post the exact search pattern you are using?– Adrian Heine
Nov 22 '12 at 9:31
The complete grep cmd I used was
grep 'delete' * -R
, but I don't think-R
is causing any issue. And yes, it normally doesn't print the non-matching ones, but not sure what's the case here...– mtk
Nov 22 '12 at 9:40
@step @everyone.. Strange... After seeing the edited post above. I tried
grep -- 'delete' *
(added--
) and it worked as expected. Removing the--
is leading to the above display.– mtk
Nov 22 '12 at 11:50
1
@Stephane Indeed you are correct. I searched the dir... and there was a file with name
-ci
. Thanks for resolving the issue. You can post this scenario as an answer.– mtk
Nov 22 '12 at 13:29