copy a line and replace text in the copied line

copy a line and replace text in the copied line

61
Advanced UserAdvanced User
61

    23:01 - Jun 26#1

    The current files have as an example:

    Code: Select all

    drop materialized view a.e_r
    /
    I want the file to have:

    Code: Select all

    drop materialized view a.e_r
    /
    drop table a.e_r
    /

    6,686585
    Grand MasterGrand Master
    6,686585

      5:07 - Jun 27#2

      That modification can be done with a Perl regular expression using backreferences with the search expression (drop materialized view )(.*\r?\n/\r?\n)(?!drop table) and the replace expression \1\2drop table \2

      .* matches any character except newline characters like carriage return or line-feed zero or more times greedy (= up to end of the line in this case).

      \r?\n is for finding optionally a carriage return and a line-feed. The expression works for that reason for files with DOS/Windows line endings (carriage return + line-feed) and with Unix line endings (just line-feed).

      The negative look-ahead (?!drop table) makes sure not inserting the two lines if there is already a drop table line next the two found and matched (=selected) lines.

      Please note that if the two lines to match are also at the end of the file, the last line of the file with the forward slash character must have also a line termination for being matched by the search expression.
      Best regards from an UC/UE/UES for Windows user from Austria

      19476
      MasterMaster
      19476

        20:38 - Jun 28#3

        Hello,

        I suggest a little modification of Mofi's solution:

        F: (^drop materialized view +)(.+)(\r?\n/\r?\n)(?!drop table \2$)
        R: \1\2\3drop table \2\3

        This regexp checks if the dropping table name is exactly same as the view name, just in case. And the match should start at the beginning of line (simple test to skip commented lines with --).
        I know I am a nitpicker :)

        BR, Fleggy