Как я могу использовать sed для удаления пустых строк без кавычек?

#string #sed

#строка #sed

Вопрос:

Я ищу sed скрипт, который преобразует

 ;; DONE Badge
;; :PROPERTIES:
;; :ID:       D0C151F1-384E-4995-B091-1EC1FE265572
;; :END:
;; [[http://api.stackexchange.com/docs/types/badge][Official documentation]]

;; [[file:~/lunch/github/vermiculus/stack-api/stack-api.org::*Badge][Badge:1]]

(defstruct stack-api/badge
  "This type represents a badge on a Stack Exchange `stack-api/site'.

Some badges, like Autobiographer, are held in common across all
Stack Exchange sites. Others, like most tag badges, vary on a
site by site basis.

Remember that ids are never guaranteed to be the same between
sites, even if a badge exists on both sites."

  (award-count nil :type integer)
  (badge-id    nil :type integer) ; refers to a badge
  (badge-type  nil :type (memq 'named
                               'tag-based))
  (description nil :type string) ; unchanged in unsafe filters
  (link        nil :type string) ; unchanged in unsafe filters
  (name        nil :type string)
  (rank        nil :type (memq 'gold
                               'silver
                               'bronze)

  (user        nil :type shallow-user))

;; Badge:1 ends here
  

в

 ;; DONE Badge
;; :PROPERTIES:
;; :ID:       D0C151F1-384E-4995-B091-1EC1FE265572
;; :END:
;; [[http://api.stackexchange.com/docs/types/badge][Official documentation]]
;; [[file:~/lunch/github/vermiculus/stack-api/stack-api.org::*Badge][Badge:1]]
(defstruct stack-api/badge
  "This type represents a badge on a Stack Exchange `stack-api/site'.

Some badges, like Autobiographer, are held in common across all
Stack Exchange sites. Others, like most tag badges, vary on a
site by site basis.

Remember that ids are never guaranteed to be the same between
sites, even if a badge exists on both sites."
  (award-count nil :type integer)
  (badge-id    nil :type integer) ; refers to a badge
  (badge-type  nil :type (memq 'named
                               'tag-based))
  (description nil :type string) ; unchanged in unsafe filters
  (link        nil :type string) ; unchanged in unsafe filters
  (name        nil :type string)
  (rank        nil :type (memq 'gold
                               'silver
                               'bronze)
  (user        nil :type shallow-user))
;; Badge:1 ends here
  

Бонусные баллы за удаление всех комментариев за один раз, но я мог бы использовать для этого конвейер. В основном я не знаю, как проверить, находится ли сканер внутри строки или нет.

Ответ №1:

Неясно, сможете ли вы когда-либо заключить две двойные кавычки в одну строку. Предполагая, что нет, тогда:

 sed -e '/"/,/"/{p;d;}' -e '/^ *$/d'
  

Первое выражение совпадает между парами строк, содержащих двойные кавычки, печатая каждую строку и удаляя ее, а затем читая следующую (пропуская второе выражение). Второе выражение удаляет пустые строки.

Если вы когда-либо получаете двойные кавычки в одной строке, тогда вам нужно:

 sed -e '/".*"/{p;d;}' -e '/"/,/"/{p;d;}' -e '/^ *$/d'