-1

Suppose I had a file that had a lot of text in it. At some point I wanted to search for the following text in that file. But not exactly this text. In fact I wanted to find any text "paragraph," that could be multiple lines, that started with a line that started with "service surfaceflinger" then had a line that ended with "zygote".

What's the Linux regex for a search that would encounter this?

service surfaceflinger /system/bin/surfaceflinger

  class main

   user system

   group graphics

   onrestart restart zygote
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Joe C
  • 2,728
  • 4
  • 30
  • 38
  • check out this thread [http://stackoverflow.com/questions/3717772/regex-grep-for-multi-line-search-needed][1] [1]: http://stackoverflow.com/questions/3717772/regex-grep-for-multi-line-search-needed – Robert Peters Jan 27 '12 at 21:04
  • Are "paragraphs" separated by spaces? So "service surfaceflinger" will always be at the beginning? – Dan Fego Jan 27 '12 at 21:04
  • My first down vote. Well in regex it's difficult to enumerate all the patterns that you've tried. I tried 10 or 12 different regexs. Maybe this is a dup. For that I'm sorry. – Joe C Feb 08 '12 at 03:51
  • By "paragraph" I just mean that there is a line that starts with "service surfaceflinger" then somewhere below it is a line that ends with "zygote". I want find all text between those occurrences including those occurrences. The first occurrence of a line ending with "zygote" after a line beginning with "surface..." would do. – Joe C Feb 08 '12 at 03:57

2 Answers2

3

Using awk:

awk '/^service surfaceflinger/,/zygote$/' file_with_a_lot_of_text

Using sed:

sed -n '/^service surfaceflinger/,/zygote$/p' file_with_a_lot_of_text

Recommended reading: sed & awk.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • I used the "sed" version of this answer and it worked for finding the text. Now suppose I wanted to add a line after that text that simply said "disabled"? I tried sed -n '/^service surfaceflinger/,/zygote$/a\ disabled' filename but that added the line with the string "disabled" in between every line of that text, not just after the last line of that text. – Joe C Feb 10 '12 at 22:29
  • @JoeC: Then that is a different question and should be asked as such! – johnsyweb Feb 11 '12 at 02:37
  • Well I already got a down-vote for asking about this stuff, so I was trying to keep a low profile. But anyway thx for the tip. ;) – Joe C Feb 13 '12 at 13:33
  • @JoeC: The down-vote wasn't from me, but I suspect it wasn't "for asking about this stuff". If you hover over the ▼ next to the question, you'll see, "This question does not show any research effort". If you included your attempt with `/a`, like you did in your email, you'd be less likely to be down-voted. – johnsyweb Feb 13 '12 at 19:11
2

Try something like this:

(?ms)^service surfaceflinger.*?zygote$

where:

(?ms) turns on multi-line and dot-all mode (. matches any character)

^ matches start of line

.*? matches any number of characters lazily

$ matches end of line

MRAB
  • 20,356
  • 6
  • 40
  • 33