How can I grep patterns only inside files with a specific file name?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I spent plenty of time searching for an existing question about my issue but I falied to find any solution for this case in specific. I apologize in advance if there is an existing thread/question solving it, and would appreciate if anyone could point me forward.
I have to search a pattern (e.g. "GENE") among numerous files in a data system. However, the data set is named as follows:
foo_1549474392_93.nwk
foo_1549474392_93.ort.final.nwk
foo_1549474392_93.ort.nwk
foo_1549474392_93.ort_reroot.nwk
These are the text files for analysis n# 93. There are 550 in total. Thing is, I have to find the pattern only in the files named like "foo_1549474392_93.nwk" (i.e.: anything + underscore + digit(s) + .nwk, with numbers going from 1 to 550, not 001 to 550). Everything else before the underline is irrelevant.
I have already tried this
grep "GENE" *'/d'.nwk
and many variations using [0-9] and so on.
Thank you very much for the help!
command-line bash grep regex wildcards
add a comment |
I spent plenty of time searching for an existing question about my issue but I falied to find any solution for this case in specific. I apologize in advance if there is an existing thread/question solving it, and would appreciate if anyone could point me forward.
I have to search a pattern (e.g. "GENE") among numerous files in a data system. However, the data set is named as follows:
foo_1549474392_93.nwk
foo_1549474392_93.ort.final.nwk
foo_1549474392_93.ort.nwk
foo_1549474392_93.ort_reroot.nwk
These are the text files for analysis n# 93. There are 550 in total. Thing is, I have to find the pattern only in the files named like "foo_1549474392_93.nwk" (i.e.: anything + underscore + digit(s) + .nwk, with numbers going from 1 to 550, not 001 to 550). Everything else before the underline is irrelevant.
I have already tried this
grep "GENE" *'/d'.nwk
and many variations using [0-9] and so on.
Thank you very much for the help!
command-line bash grep regex wildcards
So you want to includefoo_1549474392_93.nwkbut exclude the others? and the93may be one, two, or three digits?
– steeldriver
Feb 8 at 0:31
Yes, before the .nwk I need at least one digit. I came across with a solution by using grep "foo" *[0-9].nwk - worked all right for this case in specific. Thanks for your time :)
– vmanechini_jr
Feb 8 at 0:58
add a comment |
I spent plenty of time searching for an existing question about my issue but I falied to find any solution for this case in specific. I apologize in advance if there is an existing thread/question solving it, and would appreciate if anyone could point me forward.
I have to search a pattern (e.g. "GENE") among numerous files in a data system. However, the data set is named as follows:
foo_1549474392_93.nwk
foo_1549474392_93.ort.final.nwk
foo_1549474392_93.ort.nwk
foo_1549474392_93.ort_reroot.nwk
These are the text files for analysis n# 93. There are 550 in total. Thing is, I have to find the pattern only in the files named like "foo_1549474392_93.nwk" (i.e.: anything + underscore + digit(s) + .nwk, with numbers going from 1 to 550, not 001 to 550). Everything else before the underline is irrelevant.
I have already tried this
grep "GENE" *'/d'.nwk
and many variations using [0-9] and so on.
Thank you very much for the help!
command-line bash grep regex wildcards
I spent plenty of time searching for an existing question about my issue but I falied to find any solution for this case in specific. I apologize in advance if there is an existing thread/question solving it, and would appreciate if anyone could point me forward.
I have to search a pattern (e.g. "GENE") among numerous files in a data system. However, the data set is named as follows:
foo_1549474392_93.nwk
foo_1549474392_93.ort.final.nwk
foo_1549474392_93.ort.nwk
foo_1549474392_93.ort_reroot.nwk
These are the text files for analysis n# 93. There are 550 in total. Thing is, I have to find the pattern only in the files named like "foo_1549474392_93.nwk" (i.e.: anything + underscore + digit(s) + .nwk, with numbers going from 1 to 550, not 001 to 550). Everything else before the underline is irrelevant.
I have already tried this
grep "GENE" *'/d'.nwk
and many variations using [0-9] and so on.
Thank you very much for the help!
command-line bash grep regex wildcards
command-line bash grep regex wildcards
asked Feb 8 at 0:22
vmanechini_jrvmanechini_jr
84
84
So you want to includefoo_1549474392_93.nwkbut exclude the others? and the93may be one, two, or three digits?
– steeldriver
Feb 8 at 0:31
Yes, before the .nwk I need at least one digit. I came across with a solution by using grep "foo" *[0-9].nwk - worked all right for this case in specific. Thanks for your time :)
– vmanechini_jr
Feb 8 at 0:58
add a comment |
So you want to includefoo_1549474392_93.nwkbut exclude the others? and the93may be one, two, or three digits?
– steeldriver
Feb 8 at 0:31
Yes, before the .nwk I need at least one digit. I came across with a solution by using grep "foo" *[0-9].nwk - worked all right for this case in specific. Thanks for your time :)
– vmanechini_jr
Feb 8 at 0:58
So you want to include
foo_1549474392_93.nwk but exclude the others? and the 93 may be one, two, or three digits?– steeldriver
Feb 8 at 0:31
So you want to include
foo_1549474392_93.nwk but exclude the others? and the 93 may be one, two, or three digits?– steeldriver
Feb 8 at 0:31
Yes, before the .nwk I need at least one digit. I came across with a solution by using grep "foo" *[0-9].nwk - worked all right for this case in specific. Thanks for your time :)
– vmanechini_jr
Feb 8 at 0:58
Yes, before the .nwk I need at least one digit. I came across with a solution by using grep "foo" *[0-9].nwk - worked all right for this case in specific. Thanks for your time :)
– vmanechini_jr
Feb 8 at 0:58
add a comment |
1 Answer
1
active
oldest
votes
To search any file whose name matches anything + underscore + digits + .nwk, with the digits going from 1 to 550 and excluding 001, 551, etc, try:
shopt -s nullglob; grep GENE *_{1..550}.nwk
Because {1..550} expands to the numbers that you want (and only the numbers you want), the glob *_{1..550}.nwk will include only the files that you want. To assure that all the files in the expansion actually exist in the directory, we set nullglob with shopt -s nullglob.
Since you may not want the change in nullglob to affect other commands, it may be useful to use parens to put the command in a subshell.
(shopt -s nullglob; grep GENE *_{1..550}.nwk)
The change in nullglob affects only the subshell (what's inside the parens) and not anything before or after.
Example
Let's create four files with GENE, two that match your filename criteria and two that don't:
$ echo GENE | tee bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
GENE
$ ls
bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
Now, let's run our command:
$ (shopt -s nullglob; grep GENE *_{1..550}.nwk)
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
The unwanted files are excluded and the good names are found.
For comparison, let's try:
$ grep "GENE" *[0-9].nwk
bad_1224_01.nwk:GENE
bad_3456_551.nwk:GENE
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
This matches the unwanted files.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1116538%2fhow-can-i-grep-patterns-only-inside-files-with-a-specific-file-name%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
To search any file whose name matches anything + underscore + digits + .nwk, with the digits going from 1 to 550 and excluding 001, 551, etc, try:
shopt -s nullglob; grep GENE *_{1..550}.nwk
Because {1..550} expands to the numbers that you want (and only the numbers you want), the glob *_{1..550}.nwk will include only the files that you want. To assure that all the files in the expansion actually exist in the directory, we set nullglob with shopt -s nullglob.
Since you may not want the change in nullglob to affect other commands, it may be useful to use parens to put the command in a subshell.
(shopt -s nullglob; grep GENE *_{1..550}.nwk)
The change in nullglob affects only the subshell (what's inside the parens) and not anything before or after.
Example
Let's create four files with GENE, two that match your filename criteria and two that don't:
$ echo GENE | tee bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
GENE
$ ls
bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
Now, let's run our command:
$ (shopt -s nullglob; grep GENE *_{1..550}.nwk)
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
The unwanted files are excluded and the good names are found.
For comparison, let's try:
$ grep "GENE" *[0-9].nwk
bad_1224_01.nwk:GENE
bad_3456_551.nwk:GENE
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
This matches the unwanted files.
add a comment |
To search any file whose name matches anything + underscore + digits + .nwk, with the digits going from 1 to 550 and excluding 001, 551, etc, try:
shopt -s nullglob; grep GENE *_{1..550}.nwk
Because {1..550} expands to the numbers that you want (and only the numbers you want), the glob *_{1..550}.nwk will include only the files that you want. To assure that all the files in the expansion actually exist in the directory, we set nullglob with shopt -s nullglob.
Since you may not want the change in nullglob to affect other commands, it may be useful to use parens to put the command in a subshell.
(shopt -s nullglob; grep GENE *_{1..550}.nwk)
The change in nullglob affects only the subshell (what's inside the parens) and not anything before or after.
Example
Let's create four files with GENE, two that match your filename criteria and two that don't:
$ echo GENE | tee bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
GENE
$ ls
bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
Now, let's run our command:
$ (shopt -s nullglob; grep GENE *_{1..550}.nwk)
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
The unwanted files are excluded and the good names are found.
For comparison, let's try:
$ grep "GENE" *[0-9].nwk
bad_1224_01.nwk:GENE
bad_3456_551.nwk:GENE
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
This matches the unwanted files.
add a comment |
To search any file whose name matches anything + underscore + digits + .nwk, with the digits going from 1 to 550 and excluding 001, 551, etc, try:
shopt -s nullglob; grep GENE *_{1..550}.nwk
Because {1..550} expands to the numbers that you want (and only the numbers you want), the glob *_{1..550}.nwk will include only the files that you want. To assure that all the files in the expansion actually exist in the directory, we set nullglob with shopt -s nullglob.
Since you may not want the change in nullglob to affect other commands, it may be useful to use parens to put the command in a subshell.
(shopt -s nullglob; grep GENE *_{1..550}.nwk)
The change in nullglob affects only the subshell (what's inside the parens) and not anything before or after.
Example
Let's create four files with GENE, two that match your filename criteria and two that don't:
$ echo GENE | tee bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
GENE
$ ls
bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
Now, let's run our command:
$ (shopt -s nullglob; grep GENE *_{1..550}.nwk)
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
The unwanted files are excluded and the good names are found.
For comparison, let's try:
$ grep "GENE" *[0-9].nwk
bad_1224_01.nwk:GENE
bad_3456_551.nwk:GENE
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
This matches the unwanted files.
To search any file whose name matches anything + underscore + digits + .nwk, with the digits going from 1 to 550 and excluding 001, 551, etc, try:
shopt -s nullglob; grep GENE *_{1..550}.nwk
Because {1..550} expands to the numbers that you want (and only the numbers you want), the glob *_{1..550}.nwk will include only the files that you want. To assure that all the files in the expansion actually exist in the directory, we set nullglob with shopt -s nullglob.
Since you may not want the change in nullglob to affect other commands, it may be useful to use parens to put the command in a subshell.
(shopt -s nullglob; grep GENE *_{1..550}.nwk)
The change in nullglob affects only the subshell (what's inside the parens) and not anything before or after.
Example
Let's create four files with GENE, two that match your filename criteria and two that don't:
$ echo GENE | tee bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
GENE
$ ls
bad_1224_01.nwk bad_3456_551.nwk good_23456_1.nwk good_763456_550.nwk
Now, let's run our command:
$ (shopt -s nullglob; grep GENE *_{1..550}.nwk)
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
The unwanted files are excluded and the good names are found.
For comparison, let's try:
$ grep "GENE" *[0-9].nwk
bad_1224_01.nwk:GENE
bad_3456_551.nwk:GENE
good_23456_1.nwk:GENE
good_763456_550.nwk:GENE
This matches the unwanted files.
edited Feb 8 at 18:53
answered Feb 8 at 1:04
John1024John1024
10.1k2535
10.1k2535
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1116538%2fhow-can-i-grep-patterns-only-inside-files-with-a-specific-file-name%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
So you want to include
foo_1549474392_93.nwkbut exclude the others? and the93may be one, two, or three digits?– steeldriver
Feb 8 at 0:31
Yes, before the .nwk I need at least one digit. I came across with a solution by using grep "foo" *[0-9].nwk - worked all right for this case in specific. Thanks for your time :)
– vmanechini_jr
Feb 8 at 0:58