adding a column which derive from another column by an expression with awk -


how can add column after first, has values calculate expression. expression has form: (vn+v0)*vl v0 , vl first , last element of 3th column, respectively , vn nth element of column. example have text table:

1 2 3 4 10 20 30 40  100 200 300 400 

it should converted

1  1800 3 4 10  9900 30 40  100 180000 300 400 

thanks

as rule defined in question, number in last line should not 180000. should (300+3)*300=90900

this awk line work you:

 awk 'nr==fnr{c[nr]=$3;l=nr;next}{$2=($3+c[1])*c[l]}7' file file 

output is:

kent$  awk 'nr==fnr{c[nr]=$3;l=nr;next}{$2=($3+c[1])*c[l]}7' file file 1 1800 3 4 10 9900 30 40 100 90900 300 400 

edit

op wants add new column after first:

kent$  awk 'nr==fnr{c[nr]=$3;l=nr;next}{$2=($3+c[1])*c[l]" "$2}7' file file 1 1800 2 3 4 10 9900 20 30 40 100 90900 200 300 400 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -