If Condition in awk

Hi All,

I have the below Input:

1  700 1200  400      1300
2 2000 1000 2000 1500  600
3 1400  200 1000 1000 1200
4 1300  500  600       200

I want to modify the field 5 and field 4 as below.
If value in field 5 is null then value of field 4 should be in field 5 . and then the value of field 4 should change to field 2 - field 5 .

My output should look like this:

1  700 1200  300  400 1300
2 2000 1000  500 1500  600
3 1400  200  400 1000 1200
4 1300  500  700  600  200

I have tried the below code but could not bale to get results.

nawk '{
 if($5 == "") then '$5=$4';
  else
  '$5=$5';
   $4=$2-$5;
   }' sample

Could any one help me on this ?

Thanks in advance.
am24

You are mixing up shell and awk command syntax.

In awk it looks like this:

if( ... ) {
   ...
}
else {
   ...
}

There is also superfluous single quotes that will give Problems when being executed.

It might be correct this way:

nawk '
{
    if($5 == "") {
        $5=$4
    }
    else {
        $5=$5
        $4=$2-$5
    }
}
' sample
1 Like

awk can't determine empty fields with the default field separator. So make sure to find to use a reasonable one. With e.g. <TAB> as an FS, try

awk '{$5=$5?$5:$4; $4=$2-$5}1' FS="\t" OFS="\t" file
1	700	1200	300	400	1300
2	2000	1000	500	1500	600
3	1400	200	400	1000	1200
4	1300	500	700	600	200
1 Like

Hi Zaxxon,

I have tried as you suggested. Got an error message as below.

Unmatched '

Hi Rudi,
I have tried the code you suggested. here is the below output.

nawk '{$5=$5?$5:$4; $4=$2-$5}1' FS="\t" OFS="\t" sample
1  700 1200  400      1300                      0
2 2000 1000 2000 1500  600                      0
3 1400  200 1000 1000 1200                      0
4 1300  500  600       200                      0

Is there any other way to achieve the results ?

Regards,
am24

That result is impossible with your input sample in post#1.

Hi Rudi,

I agree with your comments. Here i am posting the original Input. In this input i am looking for 25th col and 24th col. I have tried the code on this as well. But not getting the results.
The output values are same as Input but with one extra column at the end containing zeros.

Input:

500  380590    8740  371850   87100  117700   88220   87570     350       0   83970     270       0  144540   44170       0   59290     220   47780  300170   80420   63180  317410  340010               0  380590  142290  238300                                                                               
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
502  127770    3030  124740   22970   35430   29360   40010      90       0   30380     120       0   47600   16540       0   20240     130   12670   96780   30990   21670  106100  112580               0  127770   52670   75100                                                                               
503  232240    3810  228430   25300   54990   56900   95050     970       0   41030     120       0   46670   68090       0   48920     250   26190  170600   61640   61350  170890  173640               0  232240  110540  121700                                                                               
504 2917920   54770 2863150  405410  756020  731830 1024660   10050       0 1665380    1490       0  663930  196930       0  164680    3270  212190 2249400  668520  241240 2676680  130640 2787280       0 2917920 1387320 1530600 1269950 1647970 1672030  657710    3400    7710  577070 2744490     170    9180  164080
505 1828230   32700 1795530  256510  432880  459310  679530    2120       0  793870       0       0  393210  212210    5050  173290    2160  246320 1401260  426970  236130 1592100  148980 1679250       0 1828230 1039230  789000  814260 1013970  794600  389420    1390    3790  639030 1703210      20    1620  123380

Output:

500  380590    8740  371850   87100  117700   88220   87570     350       0   83970     270       0  144540   44170       0   59290     220   47780  300170   80420   63180  317410  340010               0  380590  142290  238300                                                                                                                                                                                 0
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                                                                                         0
502  127770    3030  124740   22970   35430   29360   40010      90       0   30380     120       0   47600   16540       0   20240     130   12670   96780   30990   21670  106100  112580               0  127770   52670   75100                                                                                                                                                                                 0
503  232240    3810  228430   25300   54990   56900   95050     970       0   41030     120       0   46670   68090       0   48920     250   26190  170600   61640   61350  170890  173640               0  232240  110540  121700                                                                                                                                                                                 0
504 2917920   54770 2863150  405410  756020  731830 1024660   10050       0 1665380    1490       0  663930  196930       0  164680    3270  212190 2249400  668520  241240 2676680  130640 2787280       0 2917920 1387320 1530600 1269950 1647970 1672030  657710    3400    7710  577070 2744490     170    9180  164080                                                                                         0
505 1828230   32700 1795530  256510  432880  459310  679530    2120       0  793870       0       0  393210  212210    5050  173290    2160  246320 1401260  426970  236130 1592100  148980 1679250       0 1828230 1039230  789000  814260 1013970  794600  389420    1390    3790  639030 1703210      20    1620  123380                                                                                         0

Can you look into this once?

Thanks & Regards,
am24

I can't replicate the behaviour you describe. In your new input sample, $24 is NOT empty in any line. $24 already HAS the difference between $2 and $25. And, as said before, with multiple spaces as field separators, you can't detect empty fields.

I'm afraid I can't help.

If value of field is not null still you are subtracting filed 2 and field 5.

awk '{ print (NF<6 ? $1" "$2" "$3" "$2-$4" "$4" "$5 : $0)}' file
1 Like

Hi Rudi,

I am sorry. I did not notice that I have misplaced the 'Output as Input ' in my post. I have changed that now.

I have given the output which i got after applying the code:

nawk '{$25=$25?$25:$24; $24=$2-$25}1' FS="\t" OFS="\t" sampl2 > op12

Regards,
am24

---------- Post updated at 02:23 AM ---------- Previous update was at 02:18 AM ----------

The problem is each field in my Input data is containing 7 columns. Some fields are having values in all the 7 cols and some fields are having blanks in 7 cols(null).

Anyways thanks for your time on this Rudi.

I will try to do this in some other platform then.

Thanks & Regards,
am24

---------- Post updated at 02:30 AM ---------- Previous update was at 02:23 AM ----------

Hi Looney.

Thanks for the code. I have applied it but not achieved the results .

Regards,
am24

Looks like we've been talking past each other; the definition of <TAB> as the input and output field sepatator is responsible for the 0 field at EOL. For fixed widths fields this obviously doesn't work... a clear specification as the first step would have helped?
GNU awk has the FIELDWIDTHS variable which could help in your case of constant field widths. For NON-GNU awk , try

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)
         T[25] = (T[25]+0)?T[25]:T[24]
         T[24] = T[2] - T[25]
         for (i=2; i<=CNF+2; i++) printf "%8s", T
         printf RS
        }
' file
2 Likes

Thanks Rudi.

I will try the code and will let you know about the results.

Regards,
am24

---------- Post updated at 03:47 AM ---------- Previous update was at 03:26 AM ----------

Hi Rudi,

You are just amazing..!! I thank you very much.

I have tried the code and it worked without any errors. I got the results exactly which i want.

I am troubling you little more. Can you please explain me the code because i want to understand it thoroughly.

Thanks for your valuable time.

Regards,
am24

awk '
        {CNF = (length()-4)/8                                   # as NF doesn't count fields correctly, create a "corrected" NF from line length
         printf "%3s", substr ($0, 1, 3)                        # the first field can be printed independently, and must be as it deviates in format
         for (i=0; i<=CNF; i++) T[i+2] = substr ($0, 4+i*8, 8)  # fill in the T array with respective fields 8 chars long.
         T[25] = (T[25]+0)?T[25]:T[24]                          # do the requested field shifting
         T[24] = T[2] - T[25]                                   # do the requested arithmetics
         for (i=2; i<=CNF+2; i++) printf "%8s", T            # formatted print of the residual fields
         printf RS                                              # print line feed
        }
' file

Hi Rudi,

Thank you for the brief explanation .

Regards,
am24