Deleting First line and last two lines from multiple text files.



  • How i can delete first line and last two lines from multiple text file at once?



  • @R-k said in Deleting First line and last two lines from multiple text files.:

    delete first line and last two lines

    A very interesting problem. It turns out to be fairly simple. It requires use of a regex (regular expression). So the find in Files window needs the search mode set to “regular expression”.

    So with the Find in Files option the entries are:
    Find What:(?-s).+\R(?s)(.+)(?-s)(.+\R){2}
    Replace With:\1
    Use “Replace All” once the selection of files set.

    So what is occurring is:
    (?-s) we do NOT allow the . to include an end of line marker
    .+\R we grab the first line including end of line
    (?s) we change the option so the . DOES include end of line marker
    (.+) we grab everything else (initially), this is saved in group 1
    (?-s) we do NOT allow the . to include an end of line marker
    (.+\R){2} requires that we have the last 2 lines. This requires the previously saved group to give those 2 lines up.
    The replace field tells the system to return the saved group, which will now be minus the last 2 lines. The first line was already grabbed prior to group 1 being saved.

    Note, if the last line is an empty line (zero length line) this will still be counted as 1 of the 2 lines.

    Hopefully this will solve your problem. As always I suggest you have backup copies of the files being edited, and to randomly check some that were worked on just to verify the regex did what you needed.

    Terry



  • Hello, @r-k, @Terry-R and All,

    I found out an other solution, which is the opposite of the @Terry-r’s one : it deletes which is not needed, instead of rewriting which must be kept !

    However, because of some strange behaviours, due to the \A assertion, my solution requires two consecutive regex S/R :

    • The first S/R, below, will replace any possible range of empty line(s), at the very beginning of files, followed with the contents of the first non-empty line with a specific string, not yet present in files. I chose the string X_X_X_X_X_X, but any expression can be used as long as that expression does not exist in all the files scanned !

      • SEARCH (?-s)\A^\R*.+

      • REPLACE X_X_X_X_X_X

    • The second and last S/R, below, will delete the entire line X_X_X_X_X_X, with its line-break, as well as the last two non-empty lines, possibly followed with some true empty lines, at the very end of files

      • SEARCH X_X_X_X_X_X\R|(?-s).+\R.+\R*\z

      • REPLACE Leave EMPTY

    Best Regards,

    guy038



  • Dear all,

    I have a similar problem but I only want to delete the last two lines of multiple *txt files.
    I try your instructions but it does not work.

    My file 5fc0da5f-069e-44ec-961d-84d77d295a5f-image.png

    Many thanks for your help in advance!
    Huynh



  • @Kosmos-Huynh said in Deleting First line and last two lines from multiple text files.:

    but I only want to delete the last two lines of

    I was trying to alter my previous regex and decided I can do better (at least my test suggests it works).

    So using the Replace function (in your case you will likely use the “Find in Files”) we have:
    Find What:(?-s)\R(^.+)?\R(^.+)?\z
    Replace With: empty field here <— nothing in this field

    It doesn’t matters where in the file the cursor currently is as long as it is not within the last 2 lines. The “Wrap Around” option should be NOT ticked as once end of file reached it should have worked, no need to go back to start again. If using on the “Find in Files” function “Wrap Around” ticked or not probably doesn’t matter.

    So it should cater for empty lines.

    See if it solves your problem.

    Terry



  • @Terry-R said in Deleting First line and last two lines from multiple text files.:

    decided I can do better (at least my test suggests it works).

    Actually even better, I’ve proofed it a bit more and “pimped” it up.
    So we have
    Find What:(?-s)(\R(.+)?){2}\z

    Terry



  • Dear @Terry-R

    I worked as tested in a weird way. As you look at my previous figures. They treated line 178 as 6 lines. As a result, I had to change a little bit like the following:
    (?-s)(\R(.+)?){7}\z

    Many thanks



  • @Kosmos-Huynh said in Deleting First line and last two lines from multiple text files.:

    They treated line 178 as 6 lines

    My regex treated the lines exactly correctly. Your image, now that I understand it slightly better shows 1 line (178) continuing over several “pseudo” lines due to enabling “Word Wrap”, which is under the “View” menu.

    If you widen the Notepad++ window (or make it more narrow) then the apparent number of lines (as you call them) will be different. It you untick Word Wrap then you will get a different view with lines marching off (off screen) to the right.

    So you need to understand the difference between what you call a line and what the “official” stance on line is. Officially a line has an “end of line” marker, thus the \R in my regex denotes this. My regex selects “2” lines using the “\R” as the marker to determine that, thus it does exactly what was asked. To view the “end of line” markers, use the option View, Show Characters, Show All Symbols. The “end of line” marker will be viewed as 2 squares in inverse highlighting (background coloured) with the characters “CR” and “LF” inside each one. This means Carriage Return and Line Feed.

    Terry



  • @Terry-R said in Deleting First line and last two lines from multiple text files.:

    Officially a line has an “end of line” marker,

    The exception is where the last (or only) line in a file has no “end of line” marker. It will be followed though by an “end of file” marker, which in my regex is the \z.

    Terry


Log in to reply