Solved Formatting Input file using AWK

May 15, 2012 at 22:50:02
Specs: Solaris
Hi Experts,

I would like to seek your assistance and expertise in transforming the below input to a formatted output as exampled:

INPUT File:

RXDP_1 8102344
RXDP_2 908125
RXDP_3 1216578
RXDP_4 2415662
RXDP_5 9088177
RXDP_6 355418
RXDP_7 1729656
23815960

Finished 9443595
Ongoing 4540365
Failed 9832000
23815960


OUTPUT file:


RXDP_1 8102344 Finished 9443595
RXDP_2 908125 Ongoing 4540365
RXDP_3 1216578 Failed 9832000
RXDP_4 2415662
RXDP_5 9088177
RXDP_6 355418
RXDP_7 1729656
----------------------------------------------------------------
TOTAL 23815960 TOTAL 23815960


I am not quite sure if its is doable in AWK, but it is what I am using however I can't achieve the wanted format.

Any input is highly appreciated.

Regards,
Amiti


See More: Formatting Input file using AWK

Report •

#1
May 16, 2012 at 13:20:01
✔ Best Answer
I choose two read the input file into 2 different arrays and then print the arrays out in the main processing loop. It's ugly because the lines with numbers being only one field acts as the delimiter between the two arrays hence the use of the flags.

Let me know if you have any questions. Note the use of nawk for Solaris:

#!/bin/ksh

echo ""|nawk ' BEGIN { hr=0; rdxpcnt=0; larrcnt=0;
   while ( getline < "inputfile.txt" > 0 )
      {
      if(NF == 1 && hr == 0)
         {
         hr=1
         hdr=$1
         continue
         }
      if(NF == 2 && hr == 0)
         rdxp[++rdxpcnt]=$0
      if(NF == 2 && hr == 1)
         larr[++larrcnt]=$0
      if(NF == 1 && hr == 1)
         trl=$1
      }
} # end BEGIN
{ # main proc loop
for(i=1; i <= rdxpcnt; i++)
   {
   printf("%s", rdxp[i])
   if(i in larr)
     printf(" %s\n", larr[i])
   else
     printf("\n")
   }
}
END {
print "------------------------------------------------"
printf("TOTOAL %s TOTAL %s\n", hdr, trl)
} '


Report •

#2
May 16, 2012 at 19:48:52
Hi Nails,

Thank you very much for the script. It works perfectly. I need to familiarize my self more on arrays. That really resolved my concern on awk.

Regards,
Amiti


Report •

#3
May 20, 2012 at 11:19:01
Amiti:

Thank you for the "best answer" award. It is much appreciated.

I don't know what I was thinking, but a more tradtional solution builds the two arrays in the main processing loop and prints the arrays in the END section:

#!/bin/ksh

nawk ' BEGIN { hr=0; rdxpcnt=0; larrcnt=0; }
{ # main proc loop
      if(NF == 1 && hr == 0)
         {
         hr=1
         hdr=$1
         continue
         }
      if(NF == 2 && hr == 0)
         rdxp[++rdxpcnt]=$0
      if(NF == 2 && hr == 1)
         larr[++larrcnt]=$0
      if(NF == 1 && hr == 1)
         trl=$1
}
END {
for(i=1; i <= rdxpcnt; i++)
   {
   printf("%s", rdxp[i])
   if(i in larr)
     printf(" %s\n", larr[i])
   else
     printf("\n")
   }
print "------------------------------------------------"
printf("TOTOAL %s TOTAL %s\n", hdr, trl)
} ' inputfile.txt


Report •
Related Solutions


Ask Question