Как найти и удалить длинный шаблон в текстовых файлах, используя linux sed с regex - PullRequest
0 голосов
/ 29 октября 2018

Я анализирую много файлов bibtex в R для некоторого анализа данных. Однако рефераты вызывают проблемы на регулярной основе, и я хочу удалить их заранее с помощью sed.

Я нашел sed 's/Abstract\s\=\s[{][{]//' < file.bib

для успешного удаления абстрактной записи и

sed 's/[}][}]\,//' < file.bib для удаления закрывающей скобки и запятой.

Однако я никак не мог объединить их, чтобы удалить все промежуточное. Например, попытавшись:

sed 's/^Abstract\s\=\s[{][{][\s\S]*[}][}]\,$//' < file.bib

Вот так выглядит ссылка на bibtex:

@article{ ISI:000072671200001,
Author = {Edmondson, A and Moingeon, B},
Title = {{From organizational learning to the learning organization}},
Journal = {{MANAGEMENT LEARNING}},
Year = {{1998}},
Volume = {{29}},
Number = {{1}},
Pages = {{5-20}},
Month = {{MAR}},
Abstract = {{This article reviews theories of organizational learning and presents a
   framework with which to organize the literature. We argue that unit of
   analysis provides one critical distinction in the organizational
   learning literature and research objective provides another. The
   resulting two-by-two matrix contains four categories of research, which
   we have called: (2) residues (organizations as residues of past
   learning); (2) communities (organizations as collections of individuals
   who can learn and develop); (3) participation (organizational
   improvement gained through intelligent activity of individual members),
   and (4) accountability (organizational improvement gained through
   developing individuals' mental models). We also propose a distinction
   between the terms organizational learning and the learning organization.
   Our subsequent analysis identifies relationships between disparate parts
   of the literature and shows that these relationships point to individual
   mental models as a critical source of leverage for creating learning
   organizations. A brief discussion of the work of two of the most visible
   researchers in this field, Peter Senge and Chris Argyris, provides
   additional support for this type of change strategy.}},
DOI = {{10.1177/1350507698291001}},
ISSN = {{1350-5076}},
Unique-ID = {{ISI:000072671200001}},
}

И вот как я бы хотел, чтобы это выглядело:

@article{ ISI:000072671200001,
Author = {Edmondson, A and Moingeon, B},
Title = {{From organizational learning to the learning organization}},
Journal = {{MANAGEMENT LEARNING}},
Year = {{1998}},
Volume = {{29}},
Number = {{1}},
Pages = {{5-20}},
Month = {{MAR}},
DOI = {{10.1177/1350507698291001}},
ISSN = {{1350-5076}},
Unique-ID = {{ISI:000072671200001}},
}

Ответы [ 2 ]

0 голосов
/ 29 октября 2018

Это может сработать для вас (GNU sed):

sed '/^Abstract = {{/,/.*}},$/d' file

При этом используется оператор диапазона ,, который в сочетании с командой удаления d удаляет строки, начиная с Abstract = {{ и заканчивая строкой }},.

0 голосов
/ 29 октября 2018

Вы можете попробовать последовательно передавать команды sed друг другу. Примерно так:

sed 's/Abstract\s\=\s[{][{]//' < file.bib | sed 's/[}][}]\,//'

Вы также можете попробовать использовать оператор OR Regex в вашем шаблоне, например:

sed 's/Abstract\s\=\s[{][{]|[}][}]\,//' < file.bib

Любой из них должен работать. Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...