Condition on Fields

Hi All,

I am trying a shell script in which I come across the situation like below. I need some help on this.

My Input contains 40 fields. If the sum of the fields(5,6,7,8) is not equal to value of field2 then I need to take the difference of �field2 and sum of fields(5,6,7,8)� and assign this difference to any of the field in fields(5,6,7,8) which is blank so that the sum of the fields(5,6,7,8) will be equals to field2 value.

Please note that the delimiter in my input is �space� and each field contains 7 columns.

Input:

501 7368320  155610 7212710 1483970 2106680 1658560 2119110   28920       0 4896920   11330       0 1308100  429470       0  272710       0  420870 5290120 2078200  549320 6819000  189030 7179290    1120 7367200 3421740 3946580 3234590 4133730 4921880 1308170    3960   11260 1123050 7009340     480    7180  351320
513  427840       0  427840          122030  118870  141700       0       0       0    1380       0  229000       0       0  143040       0   54420  339040   88800   76260  351580   66090  361750       0  427840  192650  235190 
514  585350   42750  542600   80440  178300          203970    1370       0  202030    1470       0  181330   99970     400   55130    2580   41070  462570  122780  106520  478830   51040  534310       0  585350  311790  273560  244150  341200  202470  162340     930   20460  199150  536750       0    6310   42290
516  148070    3830  144240   21460           38030   49410     290       0   23720      90       0   45290   23430       0   33650     340   21260  117810   30260   28400  119670  118650               0  148070   67860   80210
519  324480       0  324480           69830   91430  125080       0       0       0       0       0  191500       0       0  102520       0   30460  238910   85570   77690  246790   27400  297080       0  324480  172420  152060
532  522590       0  522590          124580  125700  209100       0       0       0    1550       0  371410       0       0  109870       0   39760  429060   93530   80360  442230   33220  489370       0  522590  250120  272470                                                                               

Expected Output:

501 7368320  155610 7212710 1483970 2106680 1658560 2119110   28920       0 4896920   11330       0 1308100  429470       0  272710       0  420870 5290120 2078200  549320 6819000  189030 7179290    1120 7367200 3421740 3946580 3234590 4133730 4921880 1308170    3960   11260 1123050 7009340     480    7180  351320
513  427840       0  427840   45240  122030  118870  141700       0       0       0    1380       0  229000       0       0  143040       0   54420  339040   88800   76260  351580   66090  361750       0  427840  192650  235190 
514  585350   42750  542600   80440  178300  122640  203970    1370       0  202030    1470       0  181330   99970     400   55130    2580   41070  462570  122780  106520  478830   51040  534310       0  585350  311790  273560  244150  341200  202470  162340     930   20460  199150  536750       0    6310   42290
516  148070    3830  144240   21460   39170   38030   49410     290       0   23720      90       0   45290   23430       0   33650     340   21260  117810   30260   28400  119670  118650               0  148070   67860   80210
519  324480       0  324480   38140   69830   91430  125080       0       0       0       0       0  191500       0       0  102520       0   30460  238910   85570   77690  246790   27400  297080       0  324480  172420  152060
532  522590       0  522590   63210  124580  125700  209100       0       0       0    1550       0  371410       0       0  109870       0   39760  429060   93530   80360  442230   33220  489370       0  522590  250120  272470                                                                               

Can anyone help me with this scenario?

Thanks in advance,
am24

Would this come close to what you need:

awk '
        {CNF = (length()-4)/8
         printf "%3s", substr ($0, 1, 3)
         for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 4+i*8, 8)

         TMP = T[2] - (T[5] + T[6] + T[7] + T[8])
         if (TMP) for (i=5; i<=8; i++) if (T ~ /^ *$/) T = TMP

         for (i=2; i<=CNF+2; i++) printf "%8s", T
         printf RS

        }
' file

?

1 Like

Hi Rudi,

I have also tried a similar kind of code but i faced a problem in checking for any of the fields in (5,6,7,8) is blank .

However the code given by you solved my problem. It worked perfectly fine.

A big ton of thanks to you.

Regards,
am24