0

我有一个标量变量,其中包含文件内的一些信息。我的目标是从包含“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 的方式来做到这一点,那也可以。

4

3 回答 3

4

使用 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

您可以使用提取所有段落,除了您不想要的段落之外,使用以下单行:

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

样本输出

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

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

Ethernet2/5 is up
于 2013-08-08T17:47:42.967 回答
0

那么,您想从包含“管理向下”的行的开头删除到并包括下一个空白行(两个连续的换行符)?

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

s/= 正则表达式替换

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

administratively down= 文字文本,后跟

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

\n\n= 两个换行符

//= 替换为空(即删除)

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

于 2013-08-08T17:34:25.487 回答
0

您可以使用此模式:

(?<=\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
于 2013-08-08T17:34:32.707 回答