On Wednesday, 4 October 2017 at 15:30:08 UTC, Ali Çehreli wrote:
the hidden \r characters at the ends

Those got me too!


Here's my less than optimal solution:

int main(string[] args)
{   import std.stdio;
    import std.algorithm.iteration : map, splitter, joiner, each;
    import std.algorithm.searching : countUntil;
    import std.range : enumerate;
    import std.string : chomp;

    if (args.length != 5 && args.length != 4)
{ stderr.writeln("Something went wrong and it's obviously your fault.");
        return 1;
    }

    immutable columnID = args[2];
    immutable substitute = args[3];

    auto data = File(args[1], "r").byLine.map!chomp;
    if (data.empty)
{ stderr.writeln("input file missing\n\n(actually it exists, it's just "
                ~ "empty)\n\n(your fault regardless)");
        return 1;
    }

    File output;
    if (args.length == 5)
        output = File(args[4], "w");
    else
        output = stdout;

immutable matchedColumn = data.front.splitter(",").countUntil(columnID);
    if (matchedColumn < 0)
{ stderr.writeln("column name doesn’t exist in the input file\n\n(and "
                ~ "it's your fault)");
        return 1;
    }

    output.writeln(data.front);
    data.popFront;

    data.map!(line => line
            .splitter(",")
            .enumerate
.map!(a => a.index == matchedColumn ? substitute : a.value)
            .joiner(",")).each!(a => output.writeln(a));

    return 0;
}

I think the biggest problem is the lack of support for quoted content.

Reply via email to