Delete blank lines of multiple opened files
-
@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?
-
@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.
-
-
@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:
… 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:
becomes
Or if you have a file that ends with spaces and/or tabs, but no newline, it will still work:
becomes
----
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 makeregex 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 usingCtrl+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. - To delete only empty lines use the regex
-
@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.
-
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:
How to delete those? -
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!