Reputation: 187
Let +
and -
be the patterns and say the file is the following:
+
-
some lines 1
-
some lines 2
+
some lines 3
-
+
some lines 4
If we were specify a range in sed, say so: sed -n /+/,/-/ p
it would end up printing:
+
-
+
some lines 3
-
+
some lines 4
While the rest of it is alright, how do I not select some lines 4
as it clearly isn't in between a pair of patterns.
Edit: There can be multiple lines between the +
and -
symbols. Also, it's not an option to remove all the lines following the last -
prior to running this particular sed statement. Please keep in mind that this is a very generalized and reduced problem statement. I only need to know if there's a way to accomplish this is sed. It's hardly even snippet in the rest of the source, so anyone assuming I am out to get you to do my job, understand that that's not my intention.
Upvotes: 0
Views: 113
Reputation: 58351
This might work for you (GNU sed):
sed -n '/^+/{:a;N;/^-/M!ba;p}' file
Use seds grep-like switch -n
. Gather up lines between start and end tokens and then explicitly print them. All other lines will not be printed.
Upvotes: 0
Reputation: 88553
Save lines from +
to -
to GNU sed's hold space and only copy hold space back to pattern space if current line contains -
.
sed -n '/+/,/-/{ /+/{h;b}; H; /-/{g;p} }' file
Output:
+ - + some lines 3 -
Upvotes: 1