How to delete/replace from line X to line Y?

How to delete/replace from line X to line Y?

3
NewbieNewbie
3

    Aug 30, 2023#1

    Hello!

    I would like to delete or replace anything between two specific lines in multiple files.
    For example: Anything from line 10 to line 20.

    Thanks a lot!

    6,613548
    Grand MasterGrand Master
    6,613548

      Aug 30, 2023#2

      Replace in Files is usually best to delete a block from multiple files in a directory or a directory tree. There can be selected the block in one of these files and opened next the Replace in Files window to search for the selected block and replace all found occurrences by an empty string for deleting it from all files or the block copied and pasted with additional one or more modifications in the Replace with edit area.

      If the block to delete or replace varies a little bit from file to file, a regular expression Replace in Files may be used to delete or replace that block from all files of a directory (tree).

      The deletion/replacement of a block from each file based on line numbers instead of block content requires the usage of a much slower script or macro solution on which each file in the folder must be opened, modified in the appropriate lines range and saved one after the other. Well, for deletion/replacement of the block from line 10 to 20 a Perl regular expression Replace in Files could be also used, but not if the block to delete/modify is from line 2,032,424 to line 2,032,434.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Aug 30, 2023#3

        Hello Mofi, thanks a lot for the quick reply!

        Yes what I need is the deletion/replacement of a block from each file based on line numbers since the content is slightly different in every file.
        My files are very small, so the Perl approach will work fine, but I don't have any idea how to do that.

        6,613548
        Grand MasterGrand Master
        6,613548

          Aug 30, 2023#4

          A Perl regular expression for the deletion of the lines 10 to 20 (eleven lines in total) with ignoring the first nine lines would be \A(?:.*(?:\r?\n|\r)){9}\K(?:.*(?:\r?\n|\r)){11} and an empty replace string. There is UltraEdit for Windows v24.00 or a newer version of UltraEdit required for that regular expression meaning:

          \A ... start the search at beginning of the character stream which is the beginning of the file and the reason for minimum UltraEdit for Windows version 24.00 as older versions of UE do not support that special anchor flag.

          (?:...) ... a non-marking group with an expression inside to find a line on which the multiplier expression {9} instructs the Perl regular expression engine that the expression inside the non-marking group must be successfully applied exactly nine times for a positive match.

          .* ... matches any character except newline characters like carriage return and line-feed zero or more times.

          (?:...) .... another non-marking group for an OR expression which matches a line termination.

          \r?\n|\r ... matches an optionally once existing carriage return and a line-feed (DOS/Windows line termination with \r\n or a Unix line termination with just \n OR only a carriage return (Mac line termination with just \r as used on Mac with OS version < OS X).

          \K ... keep back the matched first nine lines which means ignore them for the replace (unselect them).

          The second half is the same with the exception of the changed multiplier eleven to match the lines ten to twenty with including line 20. Those matched lines are selected and replaced by the empty string.

          Please note that this expression does not delete any line if the file contains less than 20 lines.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Aug 30, 2023#5

            I will try this solution as soon as possible.
            Thanks a lot for the very detailed explanation.