SourceForge has been redesigned. Learn more.

Replace only in lines containing certain string

  • John Mattson

    John Mattson - 2013-07-02

    Know this has been addressed before, but cannot find it. Need to find all lines containing XXX and IFF those lines contain YYY replace YYY with ZZZ. Must be SOME way to do it other than one at a time.

    Last edit: John Mattson 2013-07-02
  • cchris

    cchris - 2013-07-06

    1/ Serch for ^(.?XXX)
    Replace with @\1 (or anything that won't possibly occur in your text)
    2/ Search for ^(@.
    Replace with \1ZZZ
    2a/ Repeat 2/ if some lines have more than 1 instance of YYY
    3/ Search for ^@
    Replace with nothing.

    What this does is to:
    1/ Mark all lines that have XXX with a leading marker (I chose @)
    2/ Locate YYY in lines with the leading marker and change them. Repeat until all occurrences are changed
    3/ Remove marker