0

I am not sure why my simple function causes "Syntax error near unexpected 'myArr,'. I taking an array, lookup key value and a line item section name as arguments. Each row in the array is "," delimited string. I am checking if the first string equals the look key value. If it does I will print out the row to my output file. I am using awk. I have the function outside of the awk being script.

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")

            }
        }       








}'
5
  • 3
    Put the awk code inside the quoted argument passed to awk and try again. No, I didn't review your stuff -- it's just that that error is from bash, not from awk. – Uncle Billy Oct 8 '19 at 14:10
  • do you mean put the function within the awk code? I am not sure you mean uncle billy. – sillyrabiit Oct 8 '19 at 14:21
  • 1
    of course, it's awk, not bash code. Paste it after the single quote, just before the BEGIN block. – Uncle Billy Oct 8 '19 at 14:30
  • Shell functions don't use parameters in it's declarations. – schrodigerscatcuriosity Oct 8 '19 at 14:54
  • Why do you have a while... loop inside your BEGIN clause? That really makes little sense. The BEGIN clause is to set up variables, etc., before the main awk loop runs across its input. – roaima Oct 8 '19 at 15:21
1

This is not really an answer -- except for moving the awk function into the awk code. More of a suggestion: I'd lay that code out like this, in a more idiomatic awk style:

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")
            }
        }       
    }
'

4
  • Thank you glenn this was helpful. I am now dealing with the files being to large to process on my unix server. It is over 1.46 GB. The server does not have enough memory to read each line and combine into one. – sillyrabiit Oct 9 '19 at 15:47
  • Sounds like you need to import the data into a database, and write some SQL – glenn jackman Oct 9 '19 at 17:47
  • If I only had that option my life would be easy. Now I am researching way to efficient read massive files using awk into any array. Any insights or tips? – sillyrabiit Oct 9 '19 at 19:54
  • Why don't you have that option? You could use a simple db like sqlite. – glenn jackman Oct 9 '19 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.