awk is changing my FS !!

Hello there,

Here is (a part) of the file I want to change. Note the three spaces before the "3" (could be "2" or "0" also).

   3 621530  1.1935E-02 631530  1.1293E+01 641530  1.1117E-02 571540  4.4419E-14
   3 581540  2.6670E-10 591540  3.8610E-09 601540  1.1016E-06 611540  3.2618E-06
   3 611541  6.5572E-07 621540  9.8307E+00 631540  3.1177E+00 641540  1.4615E+00
   3 571550  0.0000E+00 581550  8.8139E-12 591550  1.0739E-09 601550  2.5639E-07
   3 611550  1.1011E-06 621550  3.6787E-05 631550  3.4821E+00 641550  3.1855E+00
   3 641551  0.0000E+00 581560  9.4585E-13 591560  7.5358E-11 601560  1.2023E-07
   3 611560  1.7968E-07 621560  5.4237E-04 631560  3.0054E-02 641560  6.5687E+00
   3 581570  2.3926E-14 591570  9.9322E-12 601570  4.3929E-09 611570  4.6793E-07
   3 621570  5.5911E-06 631570  6.3564E-04 641570  3.2355E+00 591580  2.8608E-13

I want to find some ID (eg. 621530) and change the value following it.

I am using this script:

VALUE=".........."
awk -v var="$VALUE" 'BEGIN{}
NR==FNR{a[$0]=$0; next}
{for(i=2;i<9;i+=2)if($i in a){$(i+1)=var}}
#{$1="   "$1}
{print}' ID_to_be_changed my_file

The problem is that it removes the three spaces in front of the row if it changes something in the row...removing the commented line solves the problem.
However I do not know how to keep the same number of spaces between the rows: 2 spaces between the ID and the following value, and 1 space between a value and the ID after that (the script above turns everything into a single space).

Can someone help me?

set OFS=" " any no of spaces you want

I already tried that...
The problem then is that all my columns are spaced with only one space!

I want to keep the structure:

 
"   "3" "xxxxxx"  "x.xxxxExx" "xxxxxx"  "x.xxxxExx"...

For now, the only solution I have is:

 
VALUE=".........."
awk -v var="$VALUE" 'BEGIN{FS=OFS=" "}
NR==FNR{a[$0]=$0; next}
{for(i=4;i<9;i+=2)if($i in a){$(i+1)=var}}
{for(i=4;i<9;i+=2)if($i in a){print "  ",$1,$2," "$3,$4," "$5,$6," "$7,$8," "$9;next}}
{print}' ID_to_be_changed my_file

In fact for few lines, some values do not have the format "x.xxxxExxx", but less characters (eg "0.0"). Then, the missing characters are "spaces" in order to keep the alignement:

   2 982520  1.6134E-14 982530  1.7241E-17 982540  3.4164E-20 982550  1.2285E-24
   2 992530  1.8708E-17 992541  6.2076E-22 992540  7.5646E-20 992550  7.0917E-22
   2 162500  7.6697E-08      0  0.0000E+00      0  0.0000E+00      0  0.0000E+00
   3  10030  2.0892E-01  30060  6.0786E-04  30070  1.3046E-05  40090  1.9438E-05
   3  40100  1.1748E-04  60140  1.6964E-05 280660  0.0000E+00 290660  1.4066E-14
   3 300660  5.0403E-09 290670  7.6644E-19 300670  2.4718E-10 300680  1.1002E-12

My solution works as long as I am not changing values located on the rows with "reduced" characters...(as long as nothing is changed in the row, awk does not re-concatenate the row, thus conserving the right spacing).

Also, using all the argument in print is not convenient...

How could I solve these two problems?

You can use printf to print the line with a fixed format, something like:

VALUE=".........."
awk -v var="$VALUE" 'BEGIN{FS=OFS=" ";fmt="%5s%7s%12s%7s%12s%7s%12s%7s%12s\n"}
NR==FNR{a[$0]=$0; next}
{for(i=4;i<9;i+=2)if($i in a){$(i+1)=var}}
{for(i=4;i<9;i+=2)if($i in a){printf(fmt,$1,$2,$3,$4,$5,$6,$7,$8,$9);next}}
{print}' ID_to_be_changed my_file

Regards