awk: add a column based on the test of existing columns








E.g. we have a text file, numbers.txt, see below:
1 2
3 4
5 6

and we want to add the 3rd column, its value is yes/no. If the sum of the 1st column and the 2nd column is greater than 3, the value of the 3rd column is yes, otherwise no.
1 2 no
3 4 yes
5 6 yes

The following command:
awk '{printf "%s %s %s\n",$1,$2,($1+$2>3)?"yes":"no"}' numbers.txt > new_numbers.txt
will do the work.






No comments:

Post a Comment