Multiplying only numbers from file with fix number using awk

I have space separated file which contain string as well as numbers. I wanted to only numbers with 2.

So far i was doing

!awk '{for(i=1;i<=NF;i++) $i ~/[^0-9]+/ $i=$i*2; print $0}'

I know my print $0 statement is at wrong place but i don't know correct way.

Can someone help here ?

Upvotes: 0

Views: 167

Answers (2)

James Brown
James Brown

Reputation: 37424

Another, but using ternary operator:

$ awk '{ for(i=1; i<=NF; i++) (j=$i*2 ? j : $i) } 1' file

Upvotes: 0

karakfa
karakfa

Reputation: 67507

multiply all the numbers by 2

$ awk '{for(i=1;i<=NF;i++) if($i+0==$i) $i*=2}1'

Upvotes: 1

Related Questions