Print start and end between two patterns excluding end of range

Woeitg

I want to use sed -n "/START PATTERN/,/END PATTERN/p" file.txt pattern to search in a file.

file.txt content is

~keyword~, ~output~.
~1.~ ~output~.
~2.~ ~output~.
~keyword~, ~output~.
~1.~ ~output~.
~2.~ ~output~.
~3.~ ~output~.
~keyword blablabla~, ~not the output~.
~1.~ ~not the output~.
~2.~ ~not the output~.
~keyword blablabla2~, ~not the output~.
~1.~ ~not the output~.
~2.~ ~not the output~.
~3.~ ~not the output~.
~4.~ ~not the output~.
~blablabla~, ~not the output~.
~1.~ ~not the output~.
~2.~ ~not the output~.
~3.~ ~not the output~.
~4.~ ~not the output~.

What I expect as output is

~keyword~, ~output~.
~1.~ ~output~.
~2.~ ~output~.
~keyword~, ~output~.
~1.~ ~output~.
~2.~ ~output~.
~3.~ ~output~.

So start pattern is keyword in between ~ followed by any char . so it is /~keyword~./

End pattern is ~ followed by any alphabetic characters and then any char ..

When I run sed -n "/~keyword~./,/[~][[:alpha:]]./p" file.txt the output is

~keyword~, ~output~.
~1.~ ~output~.
~keyword~, ~output~.
~1.~ ~output~.

2nd and 3rd lines is not printing in output so my question is what is wrong with my approach? I inspired this by using the solution provided here

I also tried sed "/~keyword~./,/[~][[:alpha:]]./!d;//d" file.txt which results in empty output (inspired from this question)

This question is different with the question marked as duplicate because I asked specifily about use of sed for regular expression. Considering this, if you think it means it is duplicated, please mark it as duplicate.

don_crissti

Let's see if sed is right tool for this job:

sed '/^~[[:alpha:]].*/!{               # if line doesn't match this pattern
H                                      # append it to hold space
$!d                                    # and delete it if it's not the last line
b end                                  # else branch to label end
}
//b end                                # if line matches, branch to label end
: end                                  # label end
x                                      # exchange pattern space w. hold space
/^~keyword~.*/p                        # if pattern space matches, print it
d' infile                              # delete pattern space

With gnu sed you could write it as a one-liner:

sed '/^~[[:alpha:]].*/!{H;$!d;b end};//b end;: end;x;/^~keyword~.*/p;d' infile

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL: Proper date range between start and end date as two fields

From Dev

Replace text between START & END strings excluding the END string in perl

From Dev

Replace text between START & END strings excluding the END string in perl

From Dev

print lines between start and end using sed

From Dev

How to select all text between two patterns where the end pattern might also be a start pattern

From Dev

start date and end date between two dates

From Dev

Print data between two lines (only if "range end" exists) from a text file

From Dev

MySQL: Count days within a given range excluding weekends, include start and end if they are weekdays

From Dev

Print lines between start & end pattern, but if end pattern does not exist, don't print

From Dev

To get start date and end date of months between the given dates range

From Dev

print time intervals between start time and end time using php

From Dev

Find overlapping region between two ranges defined by their start and end points

From Dev

Get start and end of each month between two dates

From Dev

Find overlapping region between two ranges defined by their start and end points

From Dev

oracle sql start range end range with count

From Dev

oracle sql start range end range with count

From Dev

How to move lines between patterns to the end of a file

From Dev

Start and end date from date range

From Dev

Clojure range with a specific start and infinite end

From Dev

Create range of years from start to end.

From Dev

Is a /start/,/end/ range expression ever useful in awk?

From Dev

Input a double range of values with start and end values

From Dev

Can't form Range with end < start

From Dev

Start and end date from date range

From Dev

Generate start and end dates in a date range Oracle

From Dev

Script to grep on range of start/end text

From Dev

Can't form Range with end < start

From Dev

Group and count over a start and end range

From Dev

Return record with start IP and end IP as range that an IP address falls between?

Related Related

  1. 1

    MySQL: Proper date range between start and end date as two fields

  2. 2

    Replace text between START & END strings excluding the END string in perl

  3. 3

    Replace text between START & END strings excluding the END string in perl

  4. 4

    print lines between start and end using sed

  5. 5

    How to select all text between two patterns where the end pattern might also be a start pattern

  6. 6

    start date and end date between two dates

  7. 7

    Print data between two lines (only if "range end" exists) from a text file

  8. 8

    MySQL: Count days within a given range excluding weekends, include start and end if they are weekdays

  9. 9

    Print lines between start & end pattern, but if end pattern does not exist, don't print

  10. 10

    To get start date and end date of months between the given dates range

  11. 11

    print time intervals between start time and end time using php

  12. 12

    Find overlapping region between two ranges defined by their start and end points

  13. 13

    Get start and end of each month between two dates

  14. 14

    Find overlapping region between two ranges defined by their start and end points

  15. 15

    oracle sql start range end range with count

  16. 16

    oracle sql start range end range with count

  17. 17

    How to move lines between patterns to the end of a file

  18. 18

    Start and end date from date range

  19. 19

    Clojure range with a specific start and infinite end

  20. 20

    Create range of years from start to end.

  21. 21

    Is a /start/,/end/ range expression ever useful in awk?

  22. 22

    Input a double range of values with start and end values

  23. 23

    Can't form Range with end < start

  24. 24

    Start and end date from date range

  25. 25

    Generate start and end dates in a date range Oracle

  26. 26

    Script to grep on range of start/end text

  27. 27

    Can't form Range with end < start

  28. 28

    Group and count over a start and end range

  29. 29

    Return record with start IP and end IP as range that an IP address falls between?

HotTag

Archive