Awk: Print Error While Redirecting output in multiple Files

Hi,
I have a following code in which I am unable to redirect to multiple files. Can anybody please help with some corrections

 awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") {
array1[substr($1,28,14)","substr($1,126,5)]++
array2[substr($1,126,5)]++
array3[substr($1,28,14)]++
 }
else if (substr($1,26,2)=="03" && substr($1,184,14)=="MTSCC_VALFIRST")
array4[substr($1,28,14)","substr($1,126,5)]++
}
END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" } >"ValFirst_AO.txt"
 {for (counter2 in array2) { print counter2","array2[counter2] |"sort" > "ValFirst_MT.txt"} }
{for (counter3 in array3) {print counter3 ","array3[counter3]|"sort" >"ValFirst_SuccMT.txt"} }
{for (counter4 in array4) {print counter4","array4[counter4] |"sort" >"ValFirst_TotalMT.txt"} }
}
' 201505200900*
awk: cmd. line:10: END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" } >"ValFirst_AO.txt"
awk: cmd. line:10:                                                                             ^ syntax error
awk: cmd. line:11:  {for (counter2 in array2) { print counter2","array2[counter2] |"sort" > "ValFirst_MT.txt"} }
awk: cmd. line:11:                                                                        ^ syntax error
awk: cmd. line:12: {for (counter3 in array3) {print counter3 ","array3[counter3]|"sort" >"ValFirst_SuccMT.txt"} }
awk: cmd. line:12:                                                                      ^ syntax error
awk: cmd. line:13: {for (counter4 in array4) {print counter4","array4[counter4] |"sort" >"ValFirst_TotalMT.txt"} }
awk: cmd. line:13:                                                                      ^ syntax error

 

Hello siramitsharma,

Could you please change following:
Change {print counter1","array1[counter1] |"sort" }>"ValFirst_AO.txt" to {print counter1","array1[counter1] |"sort" >"ValFirst_AO.txt"} , } should be at completion of file redirection not before it.

Hope this will help you.

Thanks,
R. Singh

Still the same

 awk '{
if( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST")
{
array1[substr($1,28,14)","substr($1,126,5)]++
array2[substr($1,126,5)]++
array3[substr($1,28,14)]++
}
else if (substr($1,26,2)=="03" && substr($1,184,14)=="MTSCC_VALFIRST")
array4[substr($1,28,14)","substr($1,126,5)]++
}
END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" >"ValFirst_AO.txt" } }
' 201505200900*
awk: cmd. line:11: END { for (counter1 in array1) {print counter1","array1[counter1] |"sort" >"ValFirst_AO.txt" } }
awk: cmd. line:11:                                                                           ^ syntax error

 

Hello siramitsharma,

Not sure what you are trying to do with character | , if this is for printing purposes then try "|sort" and let us know if this helps you. Also if this advice wouldn't help you please post complete requirement details with input and expected output too.

Thanks,
R. Singh

I think you can redirect printing from awk only once. man mawk :

But you could let the system do the redirection of sort 's output for you. Try

{print counter1","array1[counter1] |"sort > ValFirst_AO.txt" }