Remove all empty lines

remove empty lines linux
remove empty lines sed
remove empty lines python
remove empty lines excel
remove empty lines vim
remove empty lines javascript
remove empty lines regex
remove empty lines word

I thought that wasn't that hard to do, but I want to remove all empty lines (or lines just containing blanks and tabs in Java) with String.replaceAll.

My regex looks like this:

s = s.replaceAll ("^[ |\t]*\n$", "");

But it doesn't work.

I looked around, but only found regexes for removing empty lines without blanks or tabs.

Try this:

String text = "line 1\n\nline 3\n\n\nline 5";
String adjusted = text.replaceAll("(?m)^[ \t]*\r?\n", "");
// ...

Note that the regex [ |\t] matches a space, a tab or a pipe char!

EDIT

B.t.w., the regex (?m)^\s+$ would also do the trick.

Remove Empty Lines – Text Mechanic, You may have spaces or tabs in your "empty" line. Use POSIX classes with sed to remove all lines containing only whitespace: Remove lines containing only whitespace. Enter your text here and click the "Remove Empty Lines" button above. Test by click the "Remove Empty Lines" button and watch these blank lines between this text disappear.

I don't know the syntax for regular expressions in Java, but /^\s*$[\n\r]{1,}/gm is the regex you're looking for.

You probably write it like this in Java:

s = s.replaceAll("(?m)^\\s*$[\n\r]{1,}", "");

I tested it with JavaScript and it works fine.

Delete empty lines using sed, Explains how to delete (remove) all empty and blank lines with sed command under Linux, macOS, FreeBSD, OpenBSD, NetBSD and Unix-like  This will remove empty lines from provided text or input string. How to Remove Empty Lines? Example. Here is a Text: Hello World. Text version: Output : Hello World.

You can remove empty lines from your code using the following code:

String test = plainTextWithEmptyLines.replaceAll("[\\\r\\\n]+","");

Here, plainTextWithEmptyLines denotes the string having the empty lines. [\\\r\\\n] is the regex pattern which is used to identify empty line breaks.

How to delete empty lines using sed command under Linux / UNIX , we also have the option to just removing all line breaks without preserving paragraph breaks. This tool enable us to save hours of manual work to remove line  All the cells in the selection that are not blank are de-selected, leaving only the blank cells selected. In the “Cells” section of the “Home” tab, click “Delete” and then select “Delete Sheet Rows” from the drop-down menu. All the blank rows are removed and the remaining rows are now contiguous.

I'm not a day-to-day Java programmer, so I'm surprised there isn't a simpler way to do this in the JDK than a regex.

Anyway,

s = s.replaceAll("\n+", "\n");

would be a bit simpler.

Update:

Sorry I missed that you wanted to also remove spaces and tabs.

s = s.replaceAll("\n[ \t]*\n", "\n");

Would work if you have consistent newlines. If not, you may want to consider making them consistent. E.g.:

s = s.replaceAll("[\n\r]+", "\n");
s = s.replaceAll("\n[ \t]*\n", "\n");

Empty Line Remover | Remove Empty Lines, Empty text line remover tool What is a empty text line remover? With this tool you can clear empty lines from any text. It can remove all completely blank lines (that​  Last but not the least, click “Replace All”. You shall see the result as below: Sometimes, you need repeat several times to remove all the extra blank lines until you achieve the effect you want. Cope with Word File Corruptions. Word is one of the most widely used software used in daily office work.

Bart Kiers's answer is missing the edge case where the last line of the string is empty or contains whitespaces.

If you try

String text = "line 1\n\nline 3\n\n\nline 5\n "; // <-- Mind the \n plus space at the end!
String adjusted = text.replaceAll("(?m)^[ \t]*\r?\n", "");

you'll get a String that equals this

"line 1\nline 3\nline 5\n " // <-- MIND the \n plus space at the end!

as result.

I expanded Bart Kiers' answer to also cover this case.

My regex pattern is:

String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)";

A little explanation:

The first part of the pattern is basically the same as Bart Kiers'. It is fine, but it does not remove an "empty" last line or a last line containing whitespaces.

That is because a last line containing just whitespaces does not end with \\r?\\n and would therefore not be matched/replaced. We need something to express this edge case. That's where the second part (after the |) comes in.

It uses a regular expression speciality: negative lookahead. That's the (?!.*\\r?\\n) part of the pattern. (?! marks the beginning of the lookahead. You could read it as: Match the regular expression before the lookahead if it is not followed by whatever is defined as string that must not follow. In our case: not any character (zero or more times) followed by a carriage-return (0 or 1 times) and a newline: .*\\r?\\n. The ) closes the lookahead. The lookahead itself is not part of the match.

If I execute the following code snippet:

String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)";
String replacement = "";
String inputString =
        "\n" +
        "Line  2 - above line is empty without spaces\n" +
        "Line  3 - next is empty without whitespaces\n" +
        "\n" +
        "Line  5 - next line is with whitespaces\n" +
        "        \n" +
        "Line  7 - next 2 lines are \"empty\". First one with whitespaces.\n" +
        "        \r\n" +
        "\n" +
        "Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line " +
        "\n" +
        "          \n" +
        "\n";

String ajdustedString = inputString.replaceAll(pattern, replacement);
System.out.println("inputString:");
System.out.println("+----");
System.out.println(inputString);
System.out.println("----+");
System.out.println("ajdustedString:");
System.out.println("+----");
System.out.print(ajdustedString); //MIND the "print" instead of "println"
System.out.println("|EOS"); //String to clearly mark the _E_nd _O_f the adjusted_S_tring
System.out.println("----+");

I get:

inputString:
+----

Line  2 - above line is empty without spaces
Line  3 - next is empty without whitespaces

Line  5 - next line is with whitespaces

Line  7 - next 2 lines are "empty". First one with whitespaces.


Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line



----+
ajdustedString:
+----
Line  2 - above line is empty without spaces
Line  3 - next is empty without whitespaces
Line  5 - next line is with whitespaces
Line  7 - next 2 lines are "empty". First one with whitespaces.
Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line |EOS
----+

If you want to learn more about lookahead/lookbehind see Regex Tutorial - Lookahead and Lookbehind Zero-Length Assertions:

Remove All Empty Lines from Text, Useful, free online tool that removes empty lines of text that contain only spaces, tabs, or newline. No ads, nonsense or garbage, just an All Hashes Calculator. With this tool you can clear empty lines from any text. It can remove all completely blank lines (that contain nothing but a newline symbol), and it can also delete lines that contain whitespace symbols, such as spaces and tabs. You can easily manage this in the settings, by checking or unchecking tabs and spaces removal mode checkbox. Textabulous!

Remove Empty Lines - Empty Text Deleter - Online, This sed line should do the trick: sed -i '/^$/d' file.txt. The -i means it will edit the file in-place. You can then perform a function to either “Remove Empty Lines” or “Remove Empty Lines (Containing Blank characters)”: The second one way is even more powerful, since it also removes blank lines formed by white space symbols (space, tab). 2.

How to remove empty/blank lines from a file in Unix (including , Screenshot of delete blank lines options in UltraEdit text editor This will delete all lines in the file that contain no characters or whitespace. If text is selected,  Tip 72 Printable Monobook Previous Next created 2001 · complexity basic · author Volker Duetsch · version 5.7 Use either of the following commands to delete all empty lines: :g/^$/d :v/./d If you want to delete all lines that are empty or that contain only whitespace characters (spaces, tabs), use either of: :g/^\\s*$/d :v/\\S/d In the second command, v operates on lines that do not match

How To Remove Blank Lines In Code Using Regular Expressions , Use either of the following commands to delete all empty lines: :g/^$/d :v/./d. If you want to delete all lines that are empty or that contain only whitespace  Remove blank lines (not including lines with spaces). grep . file.txt Remove completely blank lines (including lines with spaces). grep "\S" file.txt Note: If you get unwanted colors, that means your grep is aliases to grep --color=auto (check by type grep).

Comments
  • @Val, (?m), not (m?), enables multi line mode. Checkout this link for details: regular-expressions.info/modifiers.html
  • Regarding the edit's solution: The line-end match ($) should be removed if you wish to remove the blank line itself. Otherwise, the regex will only remove any white-space characters within the line, while still leaving the line there.
  • The example does not correctly escape the \ in Java Strings. You actually need to double escape them like this: (?m)^[ \\t]*\\r?\\n. BTW a shorter expression is: (?m)^\\s*\\n.
  • @gucce "The example does not correctly escape the \ in Java Strings" that is because there is no need to escape \ here and it was purposely used that way. \t in string literal represents tabulator character which is passed to regex engine, who has no problems with it since it is character like any other. Regex allows us to use forms like \\t or \\n or \\r (since some IDEs automatically add escaping to \ making it \\ when we copy-paste it to our code) but this doesn't mean it is preferred over \t \n or \r.
  • besides that you need to write \\s in java, this does not work :(
  • See the edit. Also consider using \s instead of a custom class defined by you and also checking for \n, \r or both as line delimiters.
  • You may wish to look into the compilation options: Pattern.UNIX_LINES, Pattern.DOTALL, and Pattern.MULTILINE.
  • @Alin Purcaru, technically speaking, things like \n are not line delimiters, but rather line terminators. The string :foo:bar: has 2 fields if colon-delimited, 3 fields if colon-terminated, and 4 fields if colon-separated. This does make a difference.
  • @tchrist By your convention I should have called them line-separators, but I considered separator and delimiter synonyms. While separator describes what \n is a little better I don't this is an issue significant enough to worth discussing. Also I can't edit my comment.
  • this will not work for lines containing whitespaces or tabs too and of course not for CRLF and LF lineendings
  • Ah, sorry. I was assuming consistent line-endings and missed that you wanted spaces and tabs removed.
  • Just searching for a regex doesn't make blank spaces disappear, does it? Nonetheless, this regex is probably all OP needs. Checking for tabs, returns is not necessary with \s as they are included in Java and matching multiple lines is useless if you are using replaceAll(). You have my vote.
  • Although you should probably edit the answer to say that. And make it about String as that is what the question asks for specifically.