Validate a csv file

前端 未结 6 502
暖寄归人
暖寄归人 2021-01-20 02:34

This is my sample file

#%cty_id1,#%ccy_id2,#%cty_src,#%cty_cd3,#%cty_nm4,#%cty_reg5,#%cty_natnl6,#%cty_bus7,#%cty_data8
690,ALL2,,AL,ALBALODMNIA,,,,
90,ALL2,,         


        
6条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 02:35

    You could try to delete the empty field in column 4, if column no. 4 is not a two-character field, as follows:

    awk 'BEGIN {FS=OFS=","}
    {
        for (i=1; i<=NF; i++) {
            if (!(i==4 && length($4)!=4))
                printf "%s%s",$i,(i

    Output:

    "id","cty_ccy_id","cty_src","cty_nm","cty_region","cty_natnl","cty_bus_load","cty_data_load"
    6,"ALL",,"AL","ALBANIA",,,,
    9,"ALL",,"AQ","ANTARCTICA",,,
    16,"IDR",,"AZ","AZERBAIJAN",,,,
    25,"LTL",,"BJ","BENIN",,,,
    26,"CVE",,"BL","SAINT BARTH�LEMY",,,,
    36,,,"BW","BOTSWANA",,,,
    41,"BNS",,"CF","CENTRAL AFRICAN REPUBLIC",,,,
    47,"CVE",,"CL","CHILE",,,,
    50,"IDR",,"CO","COLOMBIA",,,,
    61,"BNS",,"DK","DENMARK",,,,
    

    Note:

    • We use length($4)!=4 since we assume two characters in column 4, but we also have to add two extra characters for the double quotes..

提交回复
热议问题