1

I have file which contains data in columnwise manner. Please help me to get desired output. I have tried to use awk but not able to insert the value.

- A  B  C   D   E
- 6 15  78  4   
- 4 14  77  4   23
- 5 13  79  2   45
- 5 16  81  4   
- 5 16  81  4   65

I need to check if column E is blank, then if column A=6 then insert 240 in column E and A=5 then insert 120 and A=4 then insert 0 in blank space.

Expected Output:

- A B   C   D   E
- 6 15 78   4   240
- 4 14 77   4   0
- 5 13 79   2   120
- 5 16 81   4   120
- 5 16 81   4   120
  • 1
    Seems to me you're modifying E in any case, regardless of if it's empty? – ilkkachu Jun 8 '17 at 11:25
  • 1
    can we see what you tried? then we could lead you to the final answer (in a hopefully-understandable way) – Jeff Schaller Jun 8 '17 at 11:32
2

According to your expected output, the E field's emptiness does not make much influence.
It's enough to check A field value with following:

awk '{ if($2==6) $6=240; else if($2==5) $6=120; else if($2==4) $6=0 }1' file | column -t

The output:

-  A  B   C   D  E
-  6  15  78  4  240
-  4  14  77  4  0
-  5  13  79  2  120
-  5  16  81  4  120
-  5  16  81  4  120

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.