Delete blank lines of multiple opened files



  • Hey, how can i delete blank lines with blank characters of multiple opened files at the same times, i tried using a code i found in Google but didn’t work.



  • try this:
    find : ^[\t\x0B\f\n\r\x20\x85\xA0\x{2028}\x{2029}]+$\R
    replace with leave it empty
    search mode : regular expression
    and click Replace All in All Opened Documents



  • Hello, @D.A.G.-13, @abed99 and All,

    Remainder :

    • The \h ( horizontal blank character class ) = [\t\x20\xA0\x{1680}\x{2000}-\x{200B}\x{202F}\x{3000}], for Unicode encoded files

    • The \v ( vertical blank character class ) = [\n\x0b\f\r\x85\x{2028}\x{2029}] , for Unicode encoded files

    • The \s ( blank character class ) = [\t\n\x0B\f\r\x20\x85\xA0\x{1680}\x{2000}-\x{200B}\x{2028}\x{2029}\x{202F}\x{3000}] for Unicode encoded files

    Note also that :

    • The \s class is the union of the two classes \h and \v

    • The \R regex syntax represents any kind of line-break, so the regex \r\n|\v ( in this order ! ). Thus, we can write :

      • \R = \r\n|[\n\x0B\f\r\x85\x{2028}\x{2029}]

      • \R = (?>\r\n|\n|\x0b|\f|\r|\x85|\x{2028}|\x{2029}) ( The atomic syntax (?>•••••) ensures that the two-chars sequence \r\n cannot be separated, even in case of backtracking, in order to match further parts of the overall regex )


    So, from above, we can deduce three simple regexes :

    • To delete only empty lines use the regex ^\R+

    • To delete only blank lines, use the regex ^(\h+\R)+

    • To delete, either, empty or blank lines, use the regex ^(\h*\R)+

    For all these cases, the replacement zone is empty

    Best Regards,

    guy038



  • @guy038 I tried all the three codes but none of them work. I tried with replace all but still don’t work, what can i do?



  • @D-A-G-13 said in Delete blank lines of multiple opened files:

    I tried all the three codes but none of them work. I tried with replace all but still don’t work, what can i do?

    If the regex did not work then the character \h (any horizontal blank character) is not within the lines.

    First question, can you confirm the bottom right hand side of Notepad++ window shows Windows (CR LF) UTF-8. If not that then show us what you see.
    Second step to do. Select a single character on any of the lines (one that you intend to delete). So it will now have a shadow around that area. Select Plugins, Converter and select ASCII -> HEX. What is the number now shown (that character will have been replaced by a number). This is the hexadecimal equivalent of the character and will help us identify what you really have. After identifying it you can undo that change by pressing Edit, Undo.

    Terry



  • @D-A-G-13 said in Delete blank lines of multiple opened files:

    I tried all the three codes but none of them work

    Are you using Regular expression search mode?

    8c1032fe-9e22-4989-bf46-378266478104-image.png



  • @Alan-Kilborn Yes, and still didn’t work. What i’m looking is to delete just a one total blank line of various files. Ex: Some files have 660 lines + 1 that is totally blank, i’m looking to delete the blank line that’s at the pure end of the text files, like the “Remove Empty Lines (Containing Blank Characters)” option, i need something like that but that can do it for various opened files.



  • @D-A-G-13 ,

    Change the \R in any of those expressions to (\R|\Z) to match either newline or end-of-file.

    The anchors section of the user manual talks about \Z



  • @PeterJones Like… Do i change the R with a Z? Or how? Could you explain me?



  • @D-A-G-13 said in Delete blank lines of multiple opened files:

    @PeterJones Like… Do i change the R with a Z? Or how? Could you explain me?

    I thought I did explain. I mean, I literally said "change the \R in any of those expressions to (\R|\Z)". How can that be interpreted as “change the R with a Z” – I showed the literal before and after. To expand or rephrase, any place in one of the regular expressions that used to have \R should now have (\R|\Z).

    If that’s not enough, I will modify @guy038’s bullet points for you.

    • To delete only empty lines use the regex ^\R+ . For empty lines that might not have a newline because it’s the end of the file, use ^(\R|\Z)+
    • To delete only blank lines, use the regex ^(\h+\R)+ . For blank lines that might not have a newline because it’s the end of the file, use ^(\h+(\R|\Z))+
    • To delete, either, empty or blank lines, use the regex ^(\h*\R)+ . For empty or blank lines that might not have a newline because it’s the end of the file, use ^(\h*(\R|\Z))+

    Note that the second instances (my regular expressions) in each of those bullet points will work identically as @guy038’s for all lines that end in newlines, but will also allow them to work on lines at the end of the file that are missing newlines.

    However, I will say that if your next-to-last line has text and newline but your last line is empty (no newline), it won’t delete that truly empty line: it will just keep looking like this every time you hit replace:
    02c80377-e36e-449b-8784-f4d2794cbb91-image.png
    … This is because the character(s) that need to be deleted are actually the previous newline, and aren’t part of the “last” line at all. So for the most generic version, which will work at the end of the file, use ^\h*\R|\R\h*\z.

    This makes the simplification of using @guy038’s third expression, but without the group wrapper or the + quantifier, because matching one blank line and doing replace all does the same as matching multiple blank lines and doing replace all.

    The second change is that I am doing the alternate for the full row instead of making an alternate for just the end-of-line section, and allowing a newline followed by 0 or more spaces followed by the end of the file as another match.

    With that expression:
    bebcb7b4-9af7-4935-9861-fcf2be7c8dbe-image.png
    becomes
    7e8ede73-9e27-4d1b-938a-b24f7efa5707-image.png

    Or if you have a file that ends with spaces and/or tabs, but no newline, it will still work:
    4cbe018c-201b-43de-a426-1320a6232e79-image.png
    becomes
    26e1a573-5ef9-4961-a3f8-93736e6b2ea3-image.png

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.



  • @PeterJones “^\h*\R|\R\h*\z” THIS, THIS IS the code i was looking for, thanks a lot man, i just needed this code, sorry if you got frustrated of explaining, i don’t know nothing about this stuff lol, is just nice to finally seek this code. Again, thanks for answering my question.



  • @D-A-G-13 ,

    It would have been much easier if you had described the problem better, and/or given example data; when dealing with spaces and blank lines, etc, which are hard to communicate in the Forum, the best way is to use View > Show Symbols > Show All Characters (which is what I turned on during my screenshots) and then take a screenshot – this will show us the spaces, tabs, and newline characters with visible symbols.

    Also, being more precise in your problem statement will help us understand the edge cases you want to catch. If you had said, “how do I delete any blank lines, including lines that have only tabs or spaces, and including a blank line at the very end of a file?” – a phrasing like that would have helped the original responders to know that you wanted to catch the end-of-file condition as well.

    For your information, many of us don’t tend to think of that as a blank line, because the newline is the final character on the previous line; also, because so many of us always want a newline at the end of the file; many programs that use text files as input require that the last character(s) in a file will be the newline sequence! So it’s just not natural for the regulars who answer regex questions to want to delete that final line.

    In your first post, you said,

    i tried using a code i found in Google but didn’t work.

    And if you had shown us “the code you found in Google”, and explained how it didn’t work (maybe by saying "it deleted some of the blank lines, but not the one at the end of the file), that would have helped.

    In your most recent post, you said,

    i don’t know nothing about this stuff

    The best way to remedy that is to read and apply the replies you get, practice and experiment with the regular expressions, and read the resources that you’ve been linked to. If you don’t, you will never learn regex for yourself, and you will always be at the mercy and time-constraints of others…



  • Hello, @D.A.G.-13, @abed99, @alan-kilborn, @peterjones and All,

    Ah…, I see that Peter beat me at it !


    Well, in your first post you said :

    Hey, how can i delete blank lines with blank characters of multiple opened files at the same times

    But now, you say :

    i’m looking to delete the blank line that’s at the pure end of the text files

    This simplifies a bit the needed regex !

    Indeed, if you want to remove, ONLY the blank characters and, possibly, its line-break of the very last line of file(s), use the regex :

    SEARCH ^\h+\R?\z

    REPLACE Leave EMPTY


    Now, I admit that I forgot the case of the last line, with some blank characters, without any line-break at its end :-(

    So, to be complete, here are my updated regexes to use ( in addition to the @peterjones’s versions, yeah ! )

    • To delete only empty lines, anywhere in file(s), use the regex ^\R+

    • To delete only blank lines, anywhere in file(s), use the regex ^(?:\h+\R)+|\h+\z

    • To delete, either, empty and blank lines, anywhere in file(s), use the regex ^(?:\h*\R)+|\h+\z

    BR

    guy038



  • @guy038 I tried everything you posted above and while most of those blank lines and empty lines were deleted, I am left with some blank lines with white spaces / blank spaces. Here is a screenshot: Some-white-spaces-blank-spaces-not-deleted.png
    How to delete those?



  • @Ramanand-Jhingade ,

    Those are not blank lines. Those are spaces at the end of the line, that display on the next line of display, thanks to your long lines and having word wrap turned on. You can see this by the line numbering.

    Find = \h*$, Replace = empty will remove those



  • @PeterJones Thank you very much, it worked like a charm!


Log in to reply