Condition on Fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Condition on Fields
# 1  
Old 05-10-2016
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:
Code:
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:

Code:
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
# 2  
Old 05-10-2016
Would this come close to what you need:
Code:
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[i] ~ /^ *$/) T[i] = TMP

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

        }
' file

?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-11-2016
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

Join fields in a same file based on condition

I have an input file like this... All iI want to do is If the lines are identical except for the last field i want to merge them into single line input_file I feel something is nothing I feel something is everything apple mango banana apple mango grapes I want to get output like this:... (3 Replies)
Discussion started by: raj_k
3 Replies

5. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

8. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

9. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question