Question

I have a large text file that looks like:

string1
string2

string3
string4
string5

string6
string7
string8
string9

I don't care which programming language I have to use to solve it, I just want to remove lines that is only newline from that text file. I hope you have suggestion...

EDIT:

I can use Objective-C, Java, Bash, and a little C++.

The problem is if I remove all newlines or use it as a delimiter, the whole text will be compressed into one line...

Was it helpful?

Solution

grep -v '^$' oldfile > newfile

^$ is a regular expression that matches an empty line. ^ matches the beginning of the line, $ matches the end of the line, and there's nothing between them. The -v option to grep means to print all lines that don't match the regular expression.

To learn about regular expressions, go to regular-expressions.info

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top