Using grep display lines of context before back to the first line matching something else before the matched...
So say I find a match with grep -e
and I want to print all the lines from the match back to another match, like ----
for instance.
Is that possible, or do I have to use another command like awk
or something to do that?
Like for instance, if I have the following file:
----
Ticket Number: 5465415312
Software Services
Notepad
Text
Description:
My Notepad won't type text.
The output should be like:
----
Ticket Number: 5465415312
Software Services
And it matches again up back to ----
from Software Services
text-processing awk grep
add a comment |
So say I find a match with grep -e
and I want to print all the lines from the match back to another match, like ----
for instance.
Is that possible, or do I have to use another command like awk
or something to do that?
Like for instance, if I have the following file:
----
Ticket Number: 5465415312
Software Services
Notepad
Text
Description:
My Notepad won't type text.
The output should be like:
----
Ticket Number: 5465415312
Software Services
And it matches again up back to ----
from Software Services
text-processing awk grep
Please proof read. This question is very unclear, and lacks grammar. As such and answers would be guesses at what you want.
– ctrl-alt-delor
Nov 23 '18 at 23:40
What if there is no previous ---?
– Jeff Schaller
Nov 24 '18 at 0:08
@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 '18 at 0:30
Awk is probably a better tool for what you want. Hard to tell from what you posted and if your file is or is not of uniform syntax
– Panther
Nov 24 '18 at 0:38
See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 '18 at 2:41
add a comment |
So say I find a match with grep -e
and I want to print all the lines from the match back to another match, like ----
for instance.
Is that possible, or do I have to use another command like awk
or something to do that?
Like for instance, if I have the following file:
----
Ticket Number: 5465415312
Software Services
Notepad
Text
Description:
My Notepad won't type text.
The output should be like:
----
Ticket Number: 5465415312
Software Services
And it matches again up back to ----
from Software Services
text-processing awk grep
So say I find a match with grep -e
and I want to print all the lines from the match back to another match, like ----
for instance.
Is that possible, or do I have to use another command like awk
or something to do that?
Like for instance, if I have the following file:
----
Ticket Number: 5465415312
Software Services
Notepad
Text
Description:
My Notepad won't type text.
The output should be like:
----
Ticket Number: 5465415312
Software Services
And it matches again up back to ----
from Software Services
text-processing awk grep
text-processing awk grep
edited Nov 24 '18 at 0:29
leeand00
asked Nov 23 '18 at 23:38
leeand00leeand00
1,42232442
1,42232442
Please proof read. This question is very unclear, and lacks grammar. As such and answers would be guesses at what you want.
– ctrl-alt-delor
Nov 23 '18 at 23:40
What if there is no previous ---?
– Jeff Schaller
Nov 24 '18 at 0:08
@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 '18 at 0:30
Awk is probably a better tool for what you want. Hard to tell from what you posted and if your file is or is not of uniform syntax
– Panther
Nov 24 '18 at 0:38
See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 '18 at 2:41
add a comment |
Please proof read. This question is very unclear, and lacks grammar. As such and answers would be guesses at what you want.
– ctrl-alt-delor
Nov 23 '18 at 23:40
What if there is no previous ---?
– Jeff Schaller
Nov 24 '18 at 0:08
@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 '18 at 0:30
Awk is probably a better tool for what you want. Hard to tell from what you posted and if your file is or is not of uniform syntax
– Panther
Nov 24 '18 at 0:38
See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 '18 at 2:41
Please proof read. This question is very unclear, and lacks grammar. As such and answers would be guesses at what you want.
– ctrl-alt-delor
Nov 23 '18 at 23:40
Please proof read. This question is very unclear, and lacks grammar. As such and answers would be guesses at what you want.
– ctrl-alt-delor
Nov 23 '18 at 23:40
What if there is no previous ---?
– Jeff Schaller
Nov 24 '18 at 0:08
What if there is no previous ---?
– Jeff Schaller
Nov 24 '18 at 0:08
@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 '18 at 0:30
@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 '18 at 0:30
Awk is probably a better tool for what you want. Hard to tell from what you posted and if your file is or is not of uniform syntax
– Panther
Nov 24 '18 at 0:38
Awk is probably a better tool for what you want. Hard to tell from what you posted and if your file is or is not of uniform syntax
– Panther
Nov 24 '18 at 0:38
See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 '18 at 2:41
See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 '18 at 2:41
add a comment |
2 Answers
2
active
oldest
votes
sed -n '/----/,/Software Services/{p;/Software Services/q}' file
Output:
----
Ticket Number: 5465415312
Software Services
Nice use of sed, but the op is asking to print lines before or after matching a (single) pattern (I am not sure exactly) not matching multiple patterns or print lines between two patterns (a job for awk IMO).
– Panther
Nov 24 '18 at 2:38
@Panther This is thesed
way of doing it. The question implicitly talks about two patterns. One pattern to trigger output and another to delimit from where the output should start.
– Kusalananda
Feb 3 at 19:58
add a comment |
Using awk
, the short way of doing it:
awk '/^----$/,/^Software Services$/' file
The long and roundabout way of doing it:
awk '/^----$/ { lines = "" }
{ lines = (lines == "" ? $0 : lines ORS $0) }
/^Software Services$/ { print lines }' file
That is, empty the string saved in lines
every time we hit a line of four dashes. Then always append the current line to lines
, and print the contents of lines
when we find the "trigger" line in the input data.
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%2f483799%2fusing-grep-display-lines-of-context-before-back-to-the-first-line-matching-somet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed -n '/----/,/Software Services/{p;/Software Services/q}' file
Output:
----
Ticket Number: 5465415312
Software Services
Nice use of sed, but the op is asking to print lines before or after matching a (single) pattern (I am not sure exactly) not matching multiple patterns or print lines between two patterns (a job for awk IMO).
– Panther
Nov 24 '18 at 2:38
@Panther This is thesed
way of doing it. The question implicitly talks about two patterns. One pattern to trigger output and another to delimit from where the output should start.
– Kusalananda
Feb 3 at 19:58
add a comment |
sed -n '/----/,/Software Services/{p;/Software Services/q}' file
Output:
----
Ticket Number: 5465415312
Software Services
Nice use of sed, but the op is asking to print lines before or after matching a (single) pattern (I am not sure exactly) not matching multiple patterns or print lines between two patterns (a job for awk IMO).
– Panther
Nov 24 '18 at 2:38
@Panther This is thesed
way of doing it. The question implicitly talks about two patterns. One pattern to trigger output and another to delimit from where the output should start.
– Kusalananda
Feb 3 at 19:58
add a comment |
sed -n '/----/,/Software Services/{p;/Software Services/q}' file
Output:
----
Ticket Number: 5465415312
Software Services
sed -n '/----/,/Software Services/{p;/Software Services/q}' file
Output:
----
Ticket Number: 5465415312
Software Services
answered Nov 24 '18 at 1:33
CyrusCyrus
7,3812938
7,3812938
Nice use of sed, but the op is asking to print lines before or after matching a (single) pattern (I am not sure exactly) not matching multiple patterns or print lines between two patterns (a job for awk IMO).
– Panther
Nov 24 '18 at 2:38
@Panther This is thesed
way of doing it. The question implicitly talks about two patterns. One pattern to trigger output and another to delimit from where the output should start.
– Kusalananda
Feb 3 at 19:58
add a comment |
Nice use of sed, but the op is asking to print lines before or after matching a (single) pattern (I am not sure exactly) not matching multiple patterns or print lines between two patterns (a job for awk IMO).
– Panther
Nov 24 '18 at 2:38
@Panther This is thesed
way of doing it. The question implicitly talks about two patterns. One pattern to trigger output and another to delimit from where the output should start.
– Kusalananda
Feb 3 at 19:58
Nice use of sed, but the op is asking to print lines before or after matching a (single) pattern (I am not sure exactly) not matching multiple patterns or print lines between two patterns (a job for awk IMO).
– Panther
Nov 24 '18 at 2:38
Nice use of sed, but the op is asking to print lines before or after matching a (single) pattern (I am not sure exactly) not matching multiple patterns or print lines between two patterns (a job for awk IMO).
– Panther
Nov 24 '18 at 2:38
@Panther This is the
sed
way of doing it. The question implicitly talks about two patterns. One pattern to trigger output and another to delimit from where the output should start.– Kusalananda
Feb 3 at 19:58
@Panther This is the
sed
way of doing it. The question implicitly talks about two patterns. One pattern to trigger output and another to delimit from where the output should start.– Kusalananda
Feb 3 at 19:58
add a comment |
Using awk
, the short way of doing it:
awk '/^----$/,/^Software Services$/' file
The long and roundabout way of doing it:
awk '/^----$/ { lines = "" }
{ lines = (lines == "" ? $0 : lines ORS $0) }
/^Software Services$/ { print lines }' file
That is, empty the string saved in lines
every time we hit a line of four dashes. Then always append the current line to lines
, and print the contents of lines
when we find the "trigger" line in the input data.
add a comment |
Using awk
, the short way of doing it:
awk '/^----$/,/^Software Services$/' file
The long and roundabout way of doing it:
awk '/^----$/ { lines = "" }
{ lines = (lines == "" ? $0 : lines ORS $0) }
/^Software Services$/ { print lines }' file
That is, empty the string saved in lines
every time we hit a line of four dashes. Then always append the current line to lines
, and print the contents of lines
when we find the "trigger" line in the input data.
add a comment |
Using awk
, the short way of doing it:
awk '/^----$/,/^Software Services$/' file
The long and roundabout way of doing it:
awk '/^----$/ { lines = "" }
{ lines = (lines == "" ? $0 : lines ORS $0) }
/^Software Services$/ { print lines }' file
That is, empty the string saved in lines
every time we hit a line of four dashes. Then always append the current line to lines
, and print the contents of lines
when we find the "trigger" line in the input data.
Using awk
, the short way of doing it:
awk '/^----$/,/^Software Services$/' file
The long and roundabout way of doing it:
awk '/^----$/ { lines = "" }
{ lines = (lines == "" ? $0 : lines ORS $0) }
/^Software Services$/ { print lines }' file
That is, empty the string saved in lines
every time we hit a line of four dashes. Then always append the current line to lines
, and print the contents of lines
when we find the "trigger" line in the input data.
answered Feb 3 at 20:03
KusalanandaKusalananda
130k17246406
130k17246406
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%2f483799%2fusing-grep-display-lines-of-context-before-back-to-the-first-line-matching-somet%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
Please proof read. This question is very unclear, and lacks grammar. As such and answers would be guesses at what you want.
– ctrl-alt-delor
Nov 23 '18 at 23:40
What if there is no previous ---?
– Jeff Schaller
Nov 24 '18 at 0:08
@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 '18 at 0:30
Awk is probably a better tool for what you want. Hard to tell from what you posted and if your file is or is not of uniform syntax
– Panther
Nov 24 '18 at 0:38
See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 '18 at 2:41