awk 函数,意外的“myArr”附近出现语法错误

awk 函数,意外的“myArr”附近出现语法错误

我不确定为什么我的简单函数会导致“意外的 'myArr,' 附近出现语法错误”。我采用数组、查找键值和行项目部分名称作为参数。数组中的每一行都是“,”分隔的字符串。我是检查第一个字符串是否等于查找键值,如果是,我将将该行打印到我的输出文件中,我正在使用 awk 脚本之外的函数。

Authorization="Authorization.csv";
LineItem="LineItem.csv";
ModifierCode="ModifierCode.csv";

OUTFILE="output.csv"
function LineItems(myArr, AuthKey, LIsection){
        for (LI in myArr)
        {

        split(myArr[LI], a, ",")
        if (a[1] == AuthKey) 
        {
        print LIsection"," myArr[LI] > "output.csv"
        }
    }
}


awk 'BEGIN  { NR>1
 while (getline < "'"$Authorization"'" )
{
        split($0,ft,",");

        Auth_num=ft[4];
        request_date=ft[5];
        status=ft[8];
        state=ft[9];
        state_date=ft[10];
        request_source=ft[12];
        method=ft[13];
        level_of_urgency=ft[17];
        urgency_requested_by=ft[18];
        primary_diagnosis=ft[19];
        primary_code=ft[20];
        continuity_of_care=ft[21];
        court_ordered=ft[22];
        member_eligibility_and_benifits_verified=ft[23];
        single_case_agreement=ft[24];
        case_name=ft[25];
        requesting_provider=ft[28];
        requesting_provider_id=ft[29];
        requesting_provider_npi=ft[30];
        requesting_provider_phone=ft[32];
        requesting_provider_phone_ext=ft[34];
        requesting_provider_fax_number=ft[36];

        Authorization_Arr[Auth_num]=Auth_num","request_date","status","state","state_date","request_source","method","level_of_urgency","urgency_requested_by","primary_diagnosis","primary_code","continuity_of_care","court_ordered","member_eligibility_and_benifits_verified","single_case_agreement","case_name","requesting_provider","requesting_provider_id","requesting_provider_npi","requesting_provider_phone","requesting_provider_phone_ext","requesting_provider_fax_number;

}
    close("'"$Authorization"'");

    LineItCounter=0;
    while (getline < "'"$LineItem"'")
    {

        split($0,li,",");
        LineItCounter=LineItCounter+1
        LI_Auth_num=li[4];
        LI_ID=li[5];            
        line_item_number=li[6];
        requested_from_date=li[7];
        requested_to_date=li[8];
        type=li[9];
        requested_date=li[10];
        determination_due_date=li[12];
        service_type=li[13];
        status=li[14];
        level_of_urgency=li[15];
        urgency_requested_by=li[16];
        primary_procedure=li[18];
        code=li[19];
        applied_eligibility=li[20];
        servicing_provider=li[21];
        provider_id=li[22];
        npi=li[23];
        in_network=li[24];
        servicing_provider_phone=li[26];
        place_of_service=li[28];
        comments=li[29];
        method=li[30];
        out_of_network_reason=li[31];
        preliminary_requested_units=li[33];
        request_source=li[34];
        determined_from_date=li[35];
        determined_to_date=li[36];
        determination=li[37];
        requested_units=li[39];
        recommended_units=li[40];
        determined_units=li[41];
        type_of_decision=li[42];

        LineItem_Arr[LI_ID]=LI_Auth_num","LI_ID","line_item_number","requested_from_date","type","requested_date","determination_due_date","service_type","status","level_of_urgency","urgency_requested_by","primary_procedure","code","applied_eligibility","servicing_provider","provider_id","npi","in_network","servicing_provider_phone","place_of_service","comments","method","out_of_network_reason","preliminary_requested_units","request_source","determined_from_date","determined_to_date","determination","requested_units","recommended_units","determined_units","type_of_decision;    

    }
        close("'"$LineItem"'");

    Modcounter=0;
    while (getline < "'"$ModifierCode"'")
    {

        split($0,mi,",");
        Modcounter=Modcounter+1
        Mod_Auth_num=mi[4];
        Modifier_ID=mi[5];
        modifier_code=mi[7];

        ModifierCode_Arr[Modcounter]=Mod_Auth_num","Modifier_ID","modifier_code;

    }
        close("'"$ModifierCode"'");


         for (key in Authorization_Arr)
         {   
            if (key != "authorization_num") 
            { 
                print "SASH," Authorization_Arr[key] > "output.csv" 

                LineItems(LineItem_Arr,key"SPLI")
                LineItems(ModifierCode_Arr,key,"SPLI_Mod")

            }
        }       








}'

答案1

这并不是一个真正的答案——除了将 awk 函数移到 awk 代码中之外。更多的建议:我会以更惯用的 awk 风格将代码写成这样:

awk -v authFile="$Authorization" \
    -v lineFile="$LineItem" \
    -v modFile="$ModifierCode" \
    -v output="$OUTFILE" \
'
    BEGIN  {
        # build the file list dynamically
        ARGV[ARGC++] = authFile
        ARGV[ARGC++] = lineFile
        ARGV[ARGC++] = modFile
    }

    function LineItems(myArr, AuthKey, LIsection) {
        for (LI in myArr) { 
            split(myArr[LI], a, ",")
            if (a[1] == AuthKey) {
                print LIsection"," myArr[LI] > output
            }
        }
    }

    FILENAME == authFile {
        split($0,ft,",")

        Auth_num = ft[4]
        request_date = ft[5]
        status = ft[8]
        state = ft[9]
        state_date = ft[10]
        request_source = ft[12]
        method = ft[13]
        level_of_urgency = ft[17]
        urgency_requested_by = ft[18]
        primary_diagnosis = ft[19]
        primary_code = ft[20]
        continuity_of_care = ft[21]
        court_ordered = ft[22]
        member_eligibility_and_benifits_verified = ft[23]
        single_case_agreement = ft[24]
        case_name = ft[25]
        requesting_provider = ft[28]
        requesting_provider_id = ft[29]
        requesting_provider_npi = ft[30]
        requesting_provider_phone = ft[32]
        requesting_provider_phone_ext = ft[34]
        requesting_provider_fax_number = ft[36]

        Authorization_Arr[Auth_num] = Auth_num","request_date","status","state","state_date","request_source","method","level_of_urgency","urgency_requested_by","primary_diagnosis","primary_code","continuity_of_care","court_ordered","member_eligibility_and_benifits_verified","single_case_agreement","case_name","requesting_provider","requesting_provider_id","requesting_provider_npi","requesting_provider_phone","requesting_provider_phone_ext","requesting_provider_fax_number
    }

    FILENAME == lineFile {
        split($0,li,",")

        LI_Auth_num = li[4]
        LI_ID = li[5]
        line_item_number = li[6]
        requested_from_date = li[7]
        requested_to_date = li[8]
        type = li[9]
        requested_date = li[10]
        determination_due_date = li[12]
        service_type = li[13]
        status = li[14]
        level_of_urgency = li[15]
        urgency_requested_by = li[16]
        primary_procedure = li[18]
        code = li[19]
        applied_eligibility = li[20]
        servicing_provider = li[21]
        provider_id = li[22]
        npi = li[23]
        in_network = li[24]
        servicing_provider_phone = li[26]
        place_of_service = li[28]
        comments = li[29]
        method = li[30]
        out_of_network_reason = li[31]
        preliminary_requested_units = li[33]
        request_source = li[34]
        determined_from_date = li[35]
        determined_to_date = li[36]
        determination = li[37]
        requested_units = li[39]
        recommended_units = li[40]
        determined_units = li[41]
        type_of_decision = li[42]

        LineItem_Arr[LI_ID] = LI_Auth_num","LI_ID","line_item_number","requested_from_date","type","requested_date","determination_due_date","service_type","status","level_of_urgency","urgency_requested_by","primary_procedure","code","applied_eligibility","servicing_provider","provider_id","npi","in_network","servicing_provider_phone","place_of_service","comments","method","out_of_network_reason","preliminary_requested_units","request_source","determined_from_date","determined_to_date","determination","requested_units","recommended_units","determined_units","type_of_decision
    }

    FILENAME == modFile { 
        split($0,mi,",")

        Mod_Auth_num = mi[4]
        Modifier_ID = mi[5]
        modifier_code = mi[7]

        ModifierCode_Arr[FNR] = Mod_Auth_num","Modifier_ID","modifier_code
    }

    END { 
        for (key in Authorization_Arr) {   
            if (key != "authorization_num") { 
                print "SASH," Authorization_Arr[key] > output

                LineItems(LineItem_Arr, key, "SPLI")
                LineItems(ModifierCode_Arr, key, "SPLI_Mod")
            }
        }       
    }
'

相关内容