Santosh Pillai
Santosh Pillai

Reputation: 1391

multiply two columns in a tab delimited text file using awk

How can I multiply column col23 by 10 and col24 by 20 using awk? Thanks for looking at this :)

city    town    id  col1   col2   col3   col4   col21   col22   col23   col24
----------------------------------------------------------------------------------
dublin  town1   1   1       2       3       5       1       2       3       4
dublin  town1   2           2       8       10      6       7       8       9
dublin  town1   3           12      13      15      11      12      13      14
dublin  town2   4   1       2       3       5       1       2       3       4
dublin  town2   5   6       7       8       10      6       7       8       9
dublin  town2   6   11      12      13      15      1       12      13      14

I tried this but does not seem to work awk -F, '{$10*10;print}' OFS=, inputFile

Upvotes: 1

Views: 1620

Answers (1)

fedorqui
fedorqui

Reputation: 290095

As col23 is in col10 and col24 in col11, something like this can make it:

$ awk '/dublin/{$10*=10; $11*=20}1' OFS="\t" file
city    town    id  col1   col2   col3   col4   col21   col22   col23   col24
----------------------------------------------------------------------------------
dublin  town1   1       1       2       3       5       1       2       30      80
dublin  town1   2       2       8       10      6       7       8       90      0
dublin  town1   3       12      13      15      11      12      13      140     0
dublin  town2   4       1       2       3       5       1       2       30      80
dublin  town2   5       6       7       8       10      6       7       80      180
dublin  town2   6       11      12      13      15      1       12      130     280

We get the record 10 and 11 and multiply *10, *20 respectively (a=*10 is equal to a=a*10). Then we use the Output Field Separator "\t", which is a tab.

Upvotes: 4

Related Questions