Using grep display lines of context before back to the first line matching something else before the matched...











up vote
-1
down vote

favorite












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










share|improve this question
























  • 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 at 23:40












  • What if there is no previous ---?
    – Jeff Schaller
    Nov 24 at 0:08










  • @JeffSchaller I'll put one at the top.
    – leeand00
    Nov 24 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 at 0:38










  • See stackoverflow.com/questions/17988756/…
    – Panther
    Nov 24 at 2:41















up vote
-1
down vote

favorite












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










share|improve this question
























  • 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 at 23:40












  • What if there is no previous ---?
    – Jeff Schaller
    Nov 24 at 0:08










  • @JeffSchaller I'll put one at the top.
    – leeand00
    Nov 24 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 at 0:38










  • See stackoverflow.com/questions/17988756/…
    – Panther
    Nov 24 at 2:41













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 at 0:29

























asked Nov 23 at 23:38









leeand00

1,35332341




1,35332341












  • 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 at 23:40












  • What if there is no previous ---?
    – Jeff Schaller
    Nov 24 at 0:08










  • @JeffSchaller I'll put one at the top.
    – leeand00
    Nov 24 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 at 0:38










  • See stackoverflow.com/questions/17988756/…
    – Panther
    Nov 24 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 at 23:40












  • What if there is no previous ---?
    – Jeff Schaller
    Nov 24 at 0:08










  • @JeffSchaller I'll put one at the top.
    – leeand00
    Nov 24 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 at 0:38










  • See stackoverflow.com/questions/17988756/…
    – Panther
    Nov 24 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 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 at 23:40














What if there is no previous ---?
– Jeff Schaller
Nov 24 at 0:08




What if there is no previous ---?
– Jeff Schaller
Nov 24 at 0:08












@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 at 0:30




@JeffSchaller I'll put one at the top.
– leeand00
Nov 24 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 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 at 0:38












See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 at 2:41




See stackoverflow.com/questions/17988756/…
– Panther
Nov 24 at 2:41










1 Answer
1






active

oldest

votes

















up vote
1
down vote













sed -n '/----/,/Software Services/{p;/Software Services/q}' file


Output:




----
Ticket Number: 5465415312

Software Services





share|improve this answer





















  • 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 at 2:38













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
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













sed -n '/----/,/Software Services/{p;/Software Services/q}' file


Output:




----
Ticket Number: 5465415312

Software Services





share|improve this answer





















  • 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 at 2:38

















up vote
1
down vote













sed -n '/----/,/Software Services/{p;/Software Services/q}' file


Output:




----
Ticket Number: 5465415312

Software Services





share|improve this answer





















  • 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 at 2:38















up vote
1
down vote










up vote
1
down vote









sed -n '/----/,/Software Services/{p;/Software Services/q}' file


Output:




----
Ticket Number: 5465415312

Software Services





share|improve this answer












sed -n '/----/,/Software Services/{p;/Software Services/q}' file


Output:




----
Ticket Number: 5465415312

Software Services






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 at 1:33









Cyrus

7,1812835




7,1812835












  • 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 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 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 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 at 2:38




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre