delete lines before match except the one above match from stdout

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.
stdout:
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
desired result:
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
awk sed
add a comment |Â
up vote
1
down vote
favorite
Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.
stdout:
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
desired result:
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
awk sed
Perhaps edit your code to show exactly whereuniqwould fail here? It would likely be relevant to thesedanswer. (Isawketc. allowed?)
â Sparhawk
Jun 25 at 23:40
before what match? before a repeated line? before a line that repeats only in the first column?
â Jeff Schaller
Jun 26 at 0:18
perhaps your example should be a bit more complete (showing the variation in what followedetc)
â Jeff Schaller
Jun 26 at 0:19
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.
stdout:
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
desired result:
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
awk sed
Looking to use any command to produce the following outcome. Edited for greater clarity. I'm trying to remove any ^abc that is not a line before ^xyz.
stdout:
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
abc def
abc ghi
abc jkl
xyz mno
xyz pqr
desired result:
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
awk sed
edited Jun 26 at 0:35
asked Jun 25 at 23:36
Jer
235
235
Perhaps edit your code to show exactly whereuniqwould fail here? It would likely be relevant to thesedanswer. (Isawketc. allowed?)
â Sparhawk
Jun 25 at 23:40
before what match? before a repeated line? before a line that repeats only in the first column?
â Jeff Schaller
Jun 26 at 0:18
perhaps your example should be a bit more complete (showing the variation in what followedetc)
â Jeff Schaller
Jun 26 at 0:19
add a comment |Â
Perhaps edit your code to show exactly whereuniqwould fail here? It would likely be relevant to thesedanswer. (Isawketc. allowed?)
â Sparhawk
Jun 25 at 23:40
before what match? before a repeated line? before a line that repeats only in the first column?
â Jeff Schaller
Jun 26 at 0:18
perhaps your example should be a bit more complete (showing the variation in what followedetc)
â Jeff Schaller
Jun 26 at 0:19
Perhaps edit your code to show exactly where
uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)â Sparhawk
Jun 25 at 23:40
Perhaps edit your code to show exactly where
uniq would fail here? It would likely be relevant to the sed answer. (Is awk etc. allowed?)â Sparhawk
Jun 25 at 23:40
before what match? before a repeated line? before a line that repeats only in the first column?
â Jeff Schaller
Jun 26 at 0:18
before what match? before a repeated line? before a line that repeats only in the first column?
â Jeff Schaller
Jun 26 at 0:18
perhaps your example should be a bit more complete (showing the variation in what followed
etc)â Jeff Schaller
Jun 26 at 0:19
perhaps your example should be a bit more complete (showing the variation in what followed
etc)â Jeff Schaller
Jun 26 at 0:19
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:
$ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
â Jer
Jun 26 at 1:36
add a comment |Â
up vote
0
down vote
Here is an awk one-liner.
awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt
Explanation
/^abc/ prev = $0; prevabc = "true";: If the line starts withabc, don't print it, but instead store the value of the line in the variableprev, and indicated that this line started withabcby storingtruein variableprevabc./^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts withxyz, the do the following.if (prevabc == "true") print prev; prevabc = "false": If the previous line started withabc(becauseprevabc == "true"), then print the previous lineprevand resetprevabctofalse.print: Since this line started withxyz, then print this line too.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:
$ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
â Jer
Jun 26 at 1:36
add a comment |Â
up vote
1
down vote
You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:
$ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
â Jer
Jun 26 at 1:36
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:
$ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
You could maintain a two-line buffer (using a N D loop) and print the first line before deleting it only if there's a xyz in the buffer:
$ cat stdout | sed ':a; $!N; /xyz/P; D; ba'
abc jkl
xyz mno
xyz pqr
abc jkl
xyz mno
xyz pqr
answered Jun 26 at 1:00
steeldriver
30.9k34877
30.9k34877
Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
â Jer
Jun 26 at 1:36
add a comment |Â
Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
â Jer
Jun 26 at 1:36
Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
â Jer
Jun 26 at 1:36
Short and sweet; works like a charm; thank you! Now I need to study and assimilate that syntax! :)
â Jer
Jun 26 at 1:36
add a comment |Â
up vote
0
down vote
Here is an awk one-liner.
awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt
Explanation
/^abc/ prev = $0; prevabc = "true";: If the line starts withabc, don't print it, but instead store the value of the line in the variableprev, and indicated that this line started withabcby storingtruein variableprevabc./^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts withxyz, the do the following.if (prevabc == "true") print prev; prevabc = "false": If the previous line started withabc(becauseprevabc == "true"), then print the previous lineprevand resetprevabctofalse.print: Since this line started withxyz, then print this line too.
add a comment |Â
up vote
0
down vote
Here is an awk one-liner.
awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt
Explanation
/^abc/ prev = $0; prevabc = "true";: If the line starts withabc, don't print it, but instead store the value of the line in the variableprev, and indicated that this line started withabcby storingtruein variableprevabc./^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts withxyz, the do the following.if (prevabc == "true") print prev; prevabc = "false": If the previous line started withabc(becauseprevabc == "true"), then print the previous lineprevand resetprevabctofalse.print: Since this line started withxyz, then print this line too.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Here is an awk one-liner.
awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt
Explanation
/^abc/ prev = $0; prevabc = "true";: If the line starts withabc, don't print it, but instead store the value of the line in the variableprev, and indicated that this line started withabcby storingtruein variableprevabc./^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts withxyz, the do the following.if (prevabc == "true") print prev; prevabc = "false": If the previous line started withabc(becauseprevabc == "true"), then print the previous lineprevand resetprevabctofalse.print: Since this line started withxyz, then print this line too.
Here is an awk one-liner.
awk '/^abc/ prev = $0; prevabc = "true"; /^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print' file.txt
Explanation
/^abc/ prev = $0; prevabc = "true";: If the line starts withabc, don't print it, but instead store the value of the line in the variableprev, and indicated that this line started withabcby storingtruein variableprevabc./^xyz/ if (prevabc == "true") print prev; prevabc = "false" ; print: If the line starts withxyz, the do the following.if (prevabc == "true") print prev; prevabc = "false": If the previous line started withabc(becauseprevabc == "true"), then print the previous lineprevand resetprevabctofalse.print: Since this line started withxyz, then print this line too.
answered Jun 26 at 0:58
Sparhawk
8,25963287
8,25963287
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451880%2fdelete-lines-before-match-except-the-one-above-match-from-stdout%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Perhaps edit your code to show exactly where
uniqwould fail here? It would likely be relevant to thesedanswer. (Isawketc. allowed?)â Sparhawk
Jun 25 at 23:40
before what match? before a repeated line? before a line that repeats only in the first column?
â Jeff Schaller
Jun 26 at 0:18
perhaps your example should be a bit more complete (showing the variation in what followed
etc)â Jeff Schaller
Jun 26 at 0:19