starpark
starpark

Reputation: 195

perform arithmetic operation using awk

I want to perform either of the two arithmetic operation when the condition is met:

I have FILE contains:

 2,8,123,VT
 60,32,456,DU

when it's VT,

 awk -F, '{ans = sprintf ("%.0f",($1 + $2) / 10); print ans}'

when its DU,

 awk -F, '{quo = $3 / 60); print qou}'

and the answer should be added to the file. For example, after calculating above computations:

 2,8,123,VT,1
 60,32,456,DU,2

Can I use awk for this? How?

Upvotes: 1

Views: 2260

Answers (1)

anubhava
anubhava

Reputation: 784928

Consider this awk:

awk -F, -v OFS=, '$(NF-1)=="VT"{p = sprintf("%.0f",($1 + $2) / 10)}
                  $(NF-1)=="DU"{p = $3 / 60} {print $0, p}' file
2,8,123,VT,1,1
60,32,456,DU,2,7.6

Upvotes: 2

Related Questions