ivivek_ngs
ivivek_ngs

Reputation: 937

how to add a column with specific string depending on 4th column with awk

I have a file in which the 4th column has numbers.

If 4th column is greater than 2 I want to add 5th column corresponding as gain; otherwise, the 5th column will have the string loss.

Input

1   762097  6706109 6
1   7202143 7792617 3
1   8922949 9815420 1
1   10502346    11074110    3
1   11188922    12267136    1
1   12566829    13910626    3

Desired output:

1   762097  6706109 6   gain
1   7202143 7792617 3   gain
1   8922949 9815420 1   loss
1   10502346    11074110    3   gain
1   11188922    12267136    1   loss
1   12566829    13910626    4   gain

How should I do this with awk?

Upvotes: 0

Views: 74

Answers (1)

fedorqui
fedorqui

Reputation: 290525

Use awk like this:

$ awk '{print $0, ($4>2?"gain":"lose")}' file
1   762097  6706109 6 gain
1   7202143 7792617 3 gain
1   8922949 9815420 1 lose
1   10502346    11074110    3 gain
1   11188922    12267136    1 lose
1   12566829    13910626    3 gain

As you see, it is printing the full line ($0) followed by a string. This string is determined by the value of $4 using a ternary operator.

Upvotes: 2

Related Questions