How to print of the text between the last occurence of a pair of patterns?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:
StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15
I would like the output to be:
11
12
13
How can I do this with sed?
text-processing awk sed
add a comment |Â
up vote
0
down vote
favorite
I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:
StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15
I would like the output to be:
11
12
13
How can I do this with sed?
text-processing awk sed
cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
â Emilio Galarraga
Sep 26 '17 at 17:26
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:
StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15
I would like the output to be:
11
12
13
How can I do this with sed?
text-processing awk sed
I am trying to print the lines between the last occurrence of two patterns into another file using sed. For example, if file1 contains the following:
StartPattern
1
2
3
EndPattern
4
5
StartPattern
6
7
8
EndPattern
9
10
StartPattern
11
12
13
EndPattern
14
15
I would like the output to be:
11
12
13
How can I do this with sed?
text-processing awk sed
text-processing awk sed
edited Sep 26 '17 at 16:17
RomanPerekhrest
22.5k12145
22.5k12145
asked Sep 26 '17 at 16:00
A. B
224
224
cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
â Emilio Galarraga
Sep 26 '17 at 17:26
add a comment |Â
cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
â Emilio Galarraga
Sep 26 '17 at 17:26
cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
â Emilio Galarraga
Sep 26 '17 at 17:26
cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
â Emilio Galarraga
Sep 26 '17 at 17:26
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
With single awk process:
awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
f r=(r=="")? $0: r RS $0 END print r ' file > output
output
file contents:
11
12
13
Alternative tac + awk solution:
tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output
This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
â A. B
Sep 26 '17 at 16:12
@A.B, it will work even ifEndPattern
is not the last line. You need to test
â RomanPerekhrest
Sep 26 '17 at 16:14
Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
â A. B
Sep 26 '17 at 16:22
@A.B, I can not see the issue. Look here ibb.co/bTqUYk
â RomanPerekhrest
Sep 26 '17 at 16:27
add a comment |Â
up vote
0
down vote
cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
With single awk process:
awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
f r=(r=="")? $0: r RS $0 END print r ' file > output
output
file contents:
11
12
13
Alternative tac + awk solution:
tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output
This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
â A. B
Sep 26 '17 at 16:12
@A.B, it will work even ifEndPattern
is not the last line. You need to test
â RomanPerekhrest
Sep 26 '17 at 16:14
Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
â A. B
Sep 26 '17 at 16:22
@A.B, I can not see the issue. Look here ibb.co/bTqUYk
â RomanPerekhrest
Sep 26 '17 at 16:27
add a comment |Â
up vote
0
down vote
accepted
With single awk process:
awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
f r=(r=="")? $0: r RS $0 END print r ' file > output
output
file contents:
11
12
13
Alternative tac + awk solution:
tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output
This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
â A. B
Sep 26 '17 at 16:12
@A.B, it will work even ifEndPattern
is not the last line. You need to test
â RomanPerekhrest
Sep 26 '17 at 16:14
Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
â A. B
Sep 26 '17 at 16:22
@A.B, I can not see the issue. Look here ibb.co/bTqUYk
â RomanPerekhrest
Sep 26 '17 at 16:27
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
With single awk process:
awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
f r=(r=="")? $0: r RS $0 END print r ' file > output
output
file contents:
11
12
13
Alternative tac + awk solution:
tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output
With single awk process:
awk '/StartPattern/ f=1;r=""; next f && /EndPattern/f=0
f r=(r=="")? $0: r RS $0 END print r ' file > output
output
file contents:
11
12
13
Alternative tac + awk solution:
tac file | awk '/StartPattern/exit/EndPattern/f=1;nextf' | tac > output
edited Sep 26 '17 at 16:24
answered Sep 26 '17 at 16:05
RomanPerekhrest
22.5k12145
22.5k12145
This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
â A. B
Sep 26 '17 at 16:12
@A.B, it will work even ifEndPattern
is not the last line. You need to test
â RomanPerekhrest
Sep 26 '17 at 16:14
Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
â A. B
Sep 26 '17 at 16:22
@A.B, I can not see the issue. Look here ibb.co/bTqUYk
â RomanPerekhrest
Sep 26 '17 at 16:27
add a comment |Â
This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
â A. B
Sep 26 '17 at 16:12
@A.B, it will work even ifEndPattern
is not the last line. You need to test
â RomanPerekhrest
Sep 26 '17 at 16:14
Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
â A. B
Sep 26 '17 at 16:22
@A.B, I can not see the issue. Look here ibb.co/bTqUYk
â RomanPerekhrest
Sep 26 '17 at 16:27
This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
â A. B
Sep 26 '17 at 16:12
This works if the End Pattern is the last line of the file. I realize my original post (which is now editted) was misleading. How can I only extract what's in between the patterns and not the rest of the file if there are lines below the last EndPattern?
â A. B
Sep 26 '17 at 16:12
@A.B, it will work even if
EndPattern
is not the last line. You need to testâ RomanPerekhrest
Sep 26 '17 at 16:14
@A.B, it will work even if
EndPattern
is not the last line. You need to testâ RomanPerekhrest
Sep 26 '17 at 16:14
Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
â A. B
Sep 26 '17 at 16:22
Your first answer (tac+awk) prints: 11 12 13 14 15. However, your new answer (single awk) gives the correct output.
â A. B
Sep 26 '17 at 16:22
@A.B, I can not see the issue. Look here ibb.co/bTqUYk
â RomanPerekhrest
Sep 26 '17 at 16:27
@A.B, I can not see the issue. Look here ibb.co/bTqUYk
â RomanPerekhrest
Sep 26 '17 at 16:27
add a comment |Â
up vote
0
down vote
cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'
add a comment |Â
up vote
0
down vote
cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'
add a comment |Â
up vote
0
down vote
up vote
0
down vote
cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'
cat file |sed -n 'H; /^StartPattern/h; $g;p;' |sed -e '1d' -e '/EndPattern/q' |sed '$ d'
answered Sep 26 '17 at 17:37
Emilio Galarraga
32628
32628
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%2f394575%2fhow-to-print-of-the-text-between-the-last-occurence-of-a-pair-of-patterns%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
cat file|sed -n 'H; /^StartPattern/h; $g;p;' |sed '1d' |sed '/EndPattern/q' |sed '$ d'
â Emilio Galarraga
Sep 26 '17 at 17:26