Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 31, 2017 Remove all blank lines from a file | ||||
---|---|---|---|---|
| ||||
Hi All, Can some provide me a example of how to remove all blank lines from a file. From, Vino.B |
August 31, 2017 Re: Remove all blank lines from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino | 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
ubyte[] fileData;
ubyte[] writeThis;
uint lastP;
fileData = readRaw(fileName);
foreach(uint p; ubyte b;fileData)
{
if (b == '\n')
{
writeThis ~= fileData[lastP .. p];
lastP = p;
}
}
write(fileName, fileData);
|
August 31, 2017 Re: Remove all blank lines from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino | 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 Super verbose, but: import std.stdio; import std.file; import std.algorithm.iteration; enum inFilename = "in.txt"; enum outFilename = "out.txt"; void main() { immutable lines = readText(inFilename); char[] outbuffer; outbuffer.reserve(lines.length); foreach (line; lines.splitter("\n")) { if (!line.length) continue; outbuffer ~= line; outbuffer ~= "\n"; } auto outfile = File(outFilename, "w"); outfile.write(outbuffer); } |
August 31, 2017 Re: Remove all blank lines from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino | 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. |
August 31, 2017 Re: Remove all blank lines from a file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | On Thursday, 31 August 2017 at 15:48:31 UTC, Rene Zwanenburg wrote:
> 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.
Hi All,
Thank you very much, was able to resolve the issue.
From,
Vino.B
|
Copyright © 1999-2021 by the D Language Foundation