简体   繁体   中英

Notepad++ RegEx: Search two words in subsequent lines

Consider the following text

-- 1.3.6.1.4.1.16213.2.3.1.2
    xfsCDMStatusTable OBJECT-TYPE
        SYNTAX SEQUENCE OF XfsCDMStatusEntry
        MAX-ACCESS not-accessible
        STATUS current
        DESCRIPTION
            "Define the set of MIB Variables for the CDM status table."
        ::= { xfsCDMV1 2 }


    -- 1.3.6.1.4.1.16213.2.3.1.2.1
    xfsCDMStatusEntry OBJECT-TYPE
        SYNTAX XfsCDMStatusEntry
        MAX-ACCESS not-accessible
        STATUS current

Need to filter lines which contains "1.3.6.1.4.1.16213" in first line and "OBJECT-TYPE" in next line.

Output like

 -- 1.3.6.1.4.1.16213.2.3.1.2
    xfsCDMStatusTable OBJECT-TYPE
 -- 1.3.6.1.4.1.16213.2.3.1.2.1
    xfsCDMStatusEntry OBJECT-TYPE

Can someone help with Regex pattern.

Try this expression:

(?-s)^(?:(?:(?!.*?\R+.*?\bOBJECT-TYPE\b).*?$(?:\R+|$))|(^.*?\R+.*?\bOBJECT-TYPE\b.*?$))

Replace with $1

It will take an input like this:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }


-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
    SYNTAX XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current


-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }

and output the following:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE

If you'd like to only specify the lines that have the value 1.3.6.1.4.1.16213 then use the following:

(?-s)^(?:(?:(?!.*?1\.3\.6\.1\.4\.1\.16213.*?\R+.*?\bOBJECT-TYPE\b).*?$(?:\R+|$))|(^.*?\R+.*?\bOBJECT-TYPE\b.*?$))

Use this:

  • Find what: (\\-\\- 1\\.3\\.6\\.1\\.4\\.1\\.16213\\b[^\\r\\n]*[\\r\\n]+[^\\r\\n]*OBJECT-TYPE[^\\r\\n]*)([\\r\\n]+ [^\\r\\n]*)*[\\r\\n]*
  • Replace with: $1\\n
  • "Regular expression" checked
  • ". matches newline" unchecked

Content example:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }


-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
    SYNTAX XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current


-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }


-- 1.3.6.1.4.1.xxxxx.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }

After [Replace All]:

-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2.1
xfsCDMStatusEntry OBJECT-TYPE
-- 1.3.6.1.4.1.16213.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
-- 1.3.6.1.4.1.xxxxx.2.3.1.2
xfsCDMStatusTable OBJECT-TYPE
    SYNTAX SEQUENCE OF XfsCDMStatusEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION
        "Define the set of MIB Variables for the CDM status table."
    ::= { xfsCDMV1 2 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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