Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed print line from regex to regex

Tags:

regex

bash

sed

I have a file with the following text:

text
START
This
has
to
be
printed
STOP
more text

Now i want the text in between START and STOP to be printed. I wrote the following:

cat file | sed '/START/,/STOP/p'

But that does not seem to be doing what i want. Any suggestions?

like image 726
jvermeulen Avatar asked Nov 01 '12 12:11

jvermeulen


People also ask

Can I use sed with regex?

Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and to a more limited extent, vi.

What does sed S do?

Its basic concept is simple: the s command attempts to match the pattern space against the supplied regular expression regexp ; if the match is successful, then that portion of the pattern space which was matched is replaced with replacement . For details about regexp syntax see Regular Expression Addresses.

What is regex Linux?

A regular expression (regex) is a text pattern that can be used for searching and replacing. Regular expressions are similar to Unix wild cards used in globbing, but much more powerful, and can be used to search, replace and validate text.


1 Answers

If you want to print between the start and stop signs, try:

sed -n '/START/,/STOP/ { //!p }' file.txt

Results:

This
has
to
be
printed
like image 181
Steve Avatar answered Sep 23 '22 23:09

Steve