Remove All Empty Lines

Delete empty lines using sed

You may have spaces or tabs in your "empty" line. Use POSIX classes with sed to remove all lines containing only whitespace:

sed '/^[[:space:]]*$/d'

A shorter version that uses ERE, for example with gnu sed:

sed -r '/^\s*$/d'

(Note that sed does NOT support PCRE.)

UNIX: using tr to delete empty lines

tr on linux, at least, can squeeze repeated characters:

echo -ne $a
the quick
brown fox

jumps over
echo -ne $a |tr -s '\n'
the quick
brown fox
jumps over

Windows Batch: How remove all blank (or empty) lines

For /f does not process empty lines:

for /f "usebackq tokens=* delims=" %%a in ("test.txt") do (echo(%%a)>>~.txt
move /y ~.txt "test.txt"

Remove blank lines in a file using sed

Use the following sed to delete all blank lines.

sed '/./!d' cou.data

Explanation:

  • /./ matches any character, including a newline.
  • ! negates the selector, i.e. it makes the command apply to lines which do not match the selector, which in this case is the empty line(s).
  • d deletes the selected line(s).
  • cou.data is the path to the input file.

Where did you go wrong?

The following excerpt from How sed Works states:

sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.8 Then the next cycle starts for the next input line.

I've intentionally emboldened the parts which are pertinent to why your sed examples are not working. Given your examples:

  • They seem to disregard that sed reads one line at a time.
  • The trailing newlines, (\n\n and \n\n\n in your first and second example respectively), which you're trying to match don't actually exist. They've been removed by the time your regexp pattern is executed and then reinstated when the end of the script is reached.

How should I remove all the empty lines from a string

edit/update:

Swift 5.2 or later

You can use StringProtocol split method

func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Character) throws -> Bool) rethrows -> [Substring]

And pass a Character property isNewline as KeyPath. Then you just need to use joined(separator: "\n")` to concatenate your string again:

let string = "bla bla bla\n\n\nbla\nbla bla bla\n"
let lines = string.split(whereSeparator: \.isNewline)
let result = lines.joined(separator: "\n")

print(result) // "bla bla bla\nbla\nbla bla bla"

Or as an extension of StringProtocol:

extension StringProtocol {
var lines: [SubSequence] { split(whereSeparator: \.isNewline) }
var removingAllExtraNewLines: String { lines.joined(separator: "\n") }
}


string.lines  // ["bla bla bla", "bla", "bla bla bla"]
string.removingAllExtraNewLines // "bla bla bla\nbla\nbla bla bla"

remove all empty lines from text files while keeping format

  • Ctrl+H
  • Find what: \R^$
  • Replace with: LEAVE EMPTY
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

\R      : any kind of linebreak
^ : begining of line
$ : end of line

Result for given example:

apples
oranges
peaches

Remove empty line from a multi-line string with Java

Use regex (?m)^[ \t]*\r?\n" to remove empty lines:

log.info msg.replaceAll("(?m)^[ \t]*\r?\n", "");

To remain only 1 line use [\\\r\\\n]+:

log.info text.replaceAll("[\\\r\\\n]+", "");

If you want to use the value later, then assign it

text = text.replaceAll("[\\\r\\\n]+", "");

What's a quick one-liner to remove empty lines from a python string?

How about:

text = os.linesep.join([s for s in text.splitlines() if s])

where text is the string with the possible extraneous lines?



Related Topics



Leave a reply



Submit