Reputation: 13
I have a file with many columns and three rows. I am trying to multiply the only first two rows (all of the columns in these rows) by -1 to flip their signs. New to coding so bear with me please, I've been googling around for a while now and I'm still confused.
So far what I have:
awk 'NR==1 {print $0*-1} NR==2 {print $0*-1}' filename
Any guidance is much appreciated. Here's a sample of the data...
0 0.6548 0.2719 -0.7817, etc
0 -0.355659 -0.933965 -0.152743 -0.110437, etc
0 0.666817 0.231877 -0.179657 0.986756 -0.127037 0.377155, etc (3 rows, about 50 columns)
Upvotes: 1
Views: 155
Reputation: 12867
awk 'NR < 3 { for (i=1;i<=NF;i++) { $i=$i*-1 } }1' > filename.tmp && cp -f filename.tmp filename && rm -f filename.tmp
Where the number record is less than third, loop from 1 to number of fields (NF) setting the field ($i) equal to the field * -1. Use short hand 1 to print the line
I've assumed a space for the delimiter. Change accordingly if necessary with awk -F
Upvotes: 2