Get random text between two regexen
I have a file of scraped posts that looks like this:
1 Name: foo
Hello, my name is foo.
开发者_Go百科
2 Name: bar
Hello.
Multi
line
post.
3 Name: foo
Hello, bar!
I am capable of writing a regex that will match just the headers (because they have some HTML that's not allowed in posts).
How would I get a random post from this file? Just the text, no names or such.
EDIT: Note that some post numbers may be missing.
If you have Ruby(1.9+)
$ ruby -0777 -ne 'a=$_.split(/\d+\sName:.*/);puts a.reject(&:empty?).shuffle.first' file
Hello, my name is foo.
$ ruby -0777 -ne 'a=$_.split(/\d+\sName:.*/);puts a.reject(&:empty?).shuffle.first' file
Hello, bar!
精彩评论