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
text-processing awk grep
add a comment |
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
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 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
add a comment |
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
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 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
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 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
add a comment |
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
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
add a comment |
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
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
add a comment |
up vote
1
down vote
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 at 2:38
add a comment |
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
sed -n '/----/,/Software Services/{p;/Software Services/q}' file
Output:
----
Ticket Number: 5465415312
Software Services
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
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 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
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.
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.
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 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