繁体   English   中英

删除两个模式之间的线,包括两个模式

[英]Delete lines between and including two patterns

我有一个标量变量,其中包含文件内的一些信息。 我的目标是删除任何包含单词“ Administratively down”的多行条目的变量(或文件)。

格式与此类似:

Ethernet2/3 is up
... see middle ...
a blank line
VlanXXX is administratively down, line protocol is down
... a bunch of text indented by two spaces on multiple lines ...
a blank line
Ethernet2/5 is up
... same format as previously ...

我当时在想,如果我可以匹配“行政上向下”和换行符(用于空白行),则可以对变量应用一些逻辑,以删除这些行之间的行。

目前,我正在使用Perl,但是如果有人可以给我一种ios方式来执行此操作,那也将起作用。

使用Perl的段落模式

Perl使用空行作为记录分隔符的语法很少使用: -00标志; 有关详细信息,请参见perl(1)中的命令开关

例如,给定一个语料库:

Ethernet2/3 is up
... see middle ...

VlanXXX is administratively down, line protocol is down
... a bunch of text indented by two spaces on multiple lines ...

Ethernet2/5 is up

您可以使用提取物, 除非你不与下面的一行希望那些所有pargagraphs:

$ perl -00ne 'print unless /administratively down/' /tmp/corpus

样本输出

当针对您的语料库进行测试时,单线产生:

Ethernet2/3 is up
... see middle ...

Ethernet2/5 is up

因此,您要从包含“管理上向下”的行的开头删除到包括下一个空白行(两个连续的换行符)的行吗?

$log =~ s/[^\n]+administratively down.+?\n\n//s;

s/ =正则表达式替换

[^\\n]+ =任意数量的字符,不包括换行符,后跟

administratively down =文字文本,后跟

.+? =任意数量的文本(包括换行符)非贪婪地匹配,后跟

\\n\\n =两条换行符

// =不作任何替换(即删除)

s =单线模式,允许. 匹配换行符(通常不匹配)

您可以使用以下模式:

(?<=\n\n|^)(?>[^a\n]++|\n(?!\n)|a(?!dministratively down\b))*+administratively down(?>[^\n]++|\n(?!\n))*+

细节:

(?<=\n\n|^)  # preceded by a newline or the begining of the string
# all that is not "administratively down" or a blank line, details:
(?>                               # open an atomic group
    [^a\n]++                      # all that is not a "a" or a newline
  |                               # OR
    \n(?!\n)                      # a newline not followed by a newline
  |                               # OR
    a(?!dministratively down\b)   # "a" not followed by "dministratively down"
)*+                               # repeat the atomic group zero or more times
administratively down             # "administratively down" itself
# the end of the paragraph
(?>                          # open an atomic group          
    [^\n]++                  # all that is not a newline
  |                          # OR
    \n(?!\n)                 # a newline not followed by a newline
)*+                          # repeat the atomic group zero or more times

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM