Remove all blank lines from a file
Rene Zwanenburg via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Aug 31 08:48:31 PDT 2017
On Thursday, 31 August 2017 at 14:44:07 UTC, vino wrote:
> Hi All,
>
> Can some provide me a example of how to remove all blank
> lines from a file.
>
> From,
> Vino.B
This one doesn't read the entire file into memory:
import std.stdio;
import std.array;
import std.algorithm;
import std.uni;
void main(string[] args)
{
auto inputFile = File(args[1]);
auto outputFile = File(args[2], "wb");
inputFile
.byLine
.filter!(line => !line.all!isWhite)
.copy(outputFile.lockingTextWriter);
}
But if you want to replace the input file, you'd have to write to
a temp file, remove the original, then move the temp file.
More information about the Digitalmars-d-learn
mailing list