简体   繁体   English

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

[英]Delete lines between and including two patterns

I have a scalar variable that contains some information inside of a file. 我有一个标量变量,其中包含文件内的一些信息。 My goal is to strip that variable (or file) of any multi-line entry containing the words "Administratively down." 我的目标是删除任何包含单词“ Administratively down”的多行条目的变量(或文件)。

The format is similar to this: 格式与此类似:

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 ...

I was thinking that if I could match "administratively down" and a leading newline (for the blank line), I would be able to apply some logic to the variable to also remove the lines between those lines. 我当时在想,如果我可以匹配“行政上向下”和换行符(用于空白行),则可以对变量应用一些逻辑,以删除这些行之间的行。

I'm using Perl at the moment, but if anyone can give me an ios way of doing this, that would also work. 目前,我正在使用Perl,但是如果有人可以给我一种ios方式来执行此操作,那也将起作用。

Use Perl's Paragraph Mode 使用Perl的段落模式

Perl has a rarely-used syntax for using blank lines as record separators: the -00 flags; Perl使用空行作为记录分隔符的语法很少使用: -00标志; see Command Switches in perl(1) for details. 有关详细信息,请参见perl(1)中的命令开关

Example

For example, given a corpus of: 例如,给定一个语料库:

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

You can use extract all pargagraphs except the ones you don't want with the following one-liner: 您可以使用提取物, 除非你不与下面的一行希望那些所有pargagraphs:

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

Sample Output 样本输出

When tested against your corpus, the one-liner yields: 当针对您的语料库进行测试时,单线产生:

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

Ethernet2/5 is up

So, you want to delete from the beginning of a line containing "administratively down" to and including the next blank line (two consecutive newlines)? 因此,您要从包含“管理上向下”的行的开头删除到包括下一个空白行(两个连续的换行符)的行吗?

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

s/ = regex substitution s/ =正则表达式替换

[^\\n]+ = any number of characters, not including newlines, followed by [^\\n]+ =任意数量的字符,不包括换行符,后跟

administratively down = the literal text, followed by administratively down =文字文本,后跟

.+? = any amount of text, including newlines, matched non-greedily, followed by =任意数量的文本(包括换行符)非贪婪地匹配,后跟

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

// = replace with nothing (ie delete) // =不作任何替换(即删除)

s = single line mode, allows . s =单线模式,允许. to match newlines (it usually doesn't) 匹配换行符(通常不匹配)

You can use this pattern: 您可以使用以下模式:

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

details: 细节:

(?<=\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