0

I was wondering if someone could suggest how to delete text which is outside angle brackets or outside quotation marks. For example, below I would like to delete all sentences which are not within angle brackets.

<Let's get two bottles,> I said. The bottles came. I poured a little in my glass, then a glass for Brett, then filled my glass. We touched glasses.
<Want to go for a ride?> I said. <Want to ride through the town?>

I know how to delete the text within angle brackets but not the opposite. For this task I use: sed 's/<[^<]*>//g' I don't know if what I want to do can be build on this function.

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • Welcome to Stack Overflow. Please read the [About] page soon. Are the angle brackets always on the same line (open and close), or can they be split over lines? If something is inside angle brackets, it is outside quotes, and if it is inside quotes, it is outside angle brackets -- so taken at face value ('delete text which is outside angle brackets or outside quotation marks'), you simply need to delete everything. Change the 'or' to 'and' and you're back in business: did you have in mind single quotes, double quotes, both, and what about guillemets? – Jonathan Leffler Sep 13 '13 at 16:42
  • 1
    Very similar questions at http://stackoverflow.com/q/7446716 and http://stackoverflow.com/q/4503748. – glts Sep 13 '13 at 16:48
  • Thanks for your reply, Jonathan. Well, the text is either between angle brackets (as above) or between double quotes such as below: "My son" he pointed the razor at me "go west with this face and grow up with the country." So, it is really an 'either or' option. And yes, the sentences can be split over lines. – user2777190 Sep 13 '13 at 16:51
  • Split over lines -- ugh! (I'm under-caffeinated for a discussion of the finer points of negated conjunctions in English.) – Jonathan Leffler Sep 13 '13 at 17:12

1 Answers1

1

If you don't mind a plugin, my PatternsOnText plugin provides a :DeleteExcept command. With that, even your initial requirement of both brackets and quotes can be expressed succinctly:

:DeleteExcept/<[^<]*>\|"[^"]*"/
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324