Parse a csv using awk and ignoring commas inside a field

前端 未结 7 1185
抹茶落季
抹茶落季 2020-11-29 04:36

I have a csv file where each row defines a room in a given building. Along with room, each row has a floor field. What I want to extract is all floors in all buildings. <

7条回答
  •  星月不相逢
    2020-11-29 04:55

    The extra output you're getting from csv.awk is from demo code. It's intended that you use the functions within the script to do the parsing and then output it how you want.

    At the end of csv.awk is the { ... } loop which demonstrates one of the functions. It's that code that's outputting the -> 2|.

    Instead most of that, just call the parsing function and do print csv[1], csv[2].

    That part of the code would then look like:

    {
        num_fields = parse_csv($0, csv, ",", "\"", "\"", "\\n", 1);
        if (num_fields < 0) {
            printf "ERROR: %s (%d) -> %s\n", csverr, num_fields, $0;
        } else {
    #        printf "%s -> ", $0;
    #        printf "%s", num_fields;
    #        for (i = 0;i < num_fields;i++) {
    #            printf "|%s", csv[i];
    #        }
    #        printf "|\n";
            print csv[1], csv[2]
        }
    }
    

    Save it as your_script (for example).

    Do chmod +x your_script.

    And cat is unnecessary. Also, you can do sort -u instead of sort | uniq.

    Your command would then look like:

    ./yourscript Buildings.csv | sort -u > floors.csv
    

提交回复
热议问题