Grep for multiple strings, show number of lines after one of the strings (but not the other)
I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.
cat file | grep -e 'string1' -e 'string2'
works of course, but I would like to get something like
cat file | grep -e 'string1' -e -A 5 'string2'
How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?
EDIT: just got it working with awk:
cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'
shell-script text-processing grep
migrated from serverfault.com Sep 17 '15 at 9:52
This question came from our site for system and network administrators.
add a comment |
I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.
cat file | grep -e 'string1' -e 'string2'
works of course, but I would like to get something like
cat file | grep -e 'string1' -e -A 5 'string2'
How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?
EDIT: just got it working with awk:
cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'
shell-script text-processing grep
migrated from serverfault.com Sep 17 '15 at 9:52
This question came from our site for system and network administrators.
add a comment |
I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.
cat file | grep -e 'string1' -e 'string2'
works of course, but I would like to get something like
cat file | grep -e 'string1' -e -A 5 'string2'
How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?
EDIT: just got it working with awk:
cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'
shell-script text-processing grep
I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.
cat file | grep -e 'string1' -e 'string2'
works of course, but I would like to get something like
cat file | grep -e 'string1' -e -A 5 'string2'
How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?
EDIT: just got it working with awk:
cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'
shell-script text-processing grep
shell-script text-processing grep
edited Feb 15 at 5:15
Rui F Ribeiro
40.7k1479137
40.7k1479137
asked Sep 17 '15 at 9:47
wtptrs
migrated from serverfault.com Sep 17 '15 at 9:52
This question came from our site for system and network administrators.
migrated from serverfault.com Sep 17 '15 at 9:52
This question came from our site for system and network administrators.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can't do that using one go of grep
. You need to use two grep
instances :
grep 'string1' file; grep -A 5 'string2' file
If you want to run the second upon success of the first one :
grep 'string1' file && grep -A 5 'string2' file
add a comment |
about awk code
awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file
- do not
cat ... | awk
,awk can read file. - I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)
To muchif
s…awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'
– Costas
Sep 17 '15 at 11:26
Your shorted variant isawk '/string2/{c=4;do{print;getline}while(c--)}/string1/'
– Costas
Sep 17 '15 at 11:32
add a comment |
Sed solution:
sed -n '/string2/,+4{p;b;};/string1/p' file
my sed
also accept ~
instead of +
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%2f230241%2fgrep-for-multiple-strings-show-number-of-lines-after-one-of-the-strings-but-no%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't do that using one go of grep
. You need to use two grep
instances :
grep 'string1' file; grep -A 5 'string2' file
If you want to run the second upon success of the first one :
grep 'string1' file && grep -A 5 'string2' file
add a comment |
You can't do that using one go of grep
. You need to use two grep
instances :
grep 'string1' file; grep -A 5 'string2' file
If you want to run the second upon success of the first one :
grep 'string1' file && grep -A 5 'string2' file
add a comment |
You can't do that using one go of grep
. You need to use two grep
instances :
grep 'string1' file; grep -A 5 'string2' file
If you want to run the second upon success of the first one :
grep 'string1' file && grep -A 5 'string2' file
You can't do that using one go of grep
. You need to use two grep
instances :
grep 'string1' file; grep -A 5 'string2' file
If you want to run the second upon success of the first one :
grep 'string1' file && grep -A 5 'string2' file
answered Sep 17 '15 at 10:24
heemaylheemayl
35.7k376105
35.7k376105
add a comment |
add a comment |
about awk code
awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file
- do not
cat ... | awk
,awk can read file. - I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)
To muchif
s…awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'
– Costas
Sep 17 '15 at 11:26
Your shorted variant isawk '/string2/{c=4;do{print;getline}while(c--)}/string1/'
– Costas
Sep 17 '15 at 11:32
add a comment |
about awk code
awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file
- do not
cat ... | awk
,awk can read file. - I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)
To muchif
s…awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'
– Costas
Sep 17 '15 at 11:26
Your shorted variant isawk '/string2/{c=4;do{print;getline}while(c--)}/string1/'
– Costas
Sep 17 '15 at 11:32
add a comment |
about awk code
awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file
- do not
cat ... | awk
,awk can read file. - I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)
about awk code
awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file
- do not
cat ... | awk
,awk can read file. - I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)
answered Sep 17 '15 at 10:46
ArchemarArchemar
20.2k93772
20.2k93772
To muchif
s…awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'
– Costas
Sep 17 '15 at 11:26
Your shorted variant isawk '/string2/{c=4;do{print;getline}while(c--)}/string1/'
– Costas
Sep 17 '15 at 11:32
add a comment |
To muchif
s…awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'
– Costas
Sep 17 '15 at 11:26
Your shorted variant isawk '/string2/{c=4;do{print;getline}while(c--)}/string1/'
– Costas
Sep 17 '15 at 11:32
To much
if
s… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'
– Costas
Sep 17 '15 at 11:26
To much
if
s… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'
– Costas
Sep 17 '15 at 11:26
Your shorted variant is
awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'
– Costas
Sep 17 '15 at 11:32
Your shorted variant is
awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'
– Costas
Sep 17 '15 at 11:32
add a comment |
Sed solution:
sed -n '/string2/,+4{p;b;};/string1/p' file
my sed
also accept ~
instead of +
add a comment |
Sed solution:
sed -n '/string2/,+4{p;b;};/string1/p' file
my sed
also accept ~
instead of +
add a comment |
Sed solution:
sed -n '/string2/,+4{p;b;};/string1/p' file
my sed
also accept ~
instead of +
Sed solution:
sed -n '/string2/,+4{p;b;};/string1/p' file
my sed
also accept ~
instead of +
answered Sep 17 '15 at 11:44
CostasCostas
12.7k1129
12.7k1129
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%2f230241%2fgrep-for-multiple-strings-show-number-of-lines-after-one-of-the-strings-but-no%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