Pattern-based substitution in the txt via bash : 

I have a long text file where somewhere near the end there are 1 line, where the 3 column == OXT.

    ATOM   2439  O   LEU   300      -4.699  34.599  65.335  1.00 83.23           O
    ATOM   2440  N   LEU   301      -6.822  33.898  65.057  1.00 19.70           N
    ATOM   2441  CA  LEU   301      -7.080  34.965  64.138  1.00 19.70           C
    ATOM   2442  CB  LEU   301      -8.165  34.630  63.101  1.00 19.70           C
    ATOM   2443  CG  LEU   301      -7.762  33.478  62.162  1.00 19.70           C
    ATOM   2444  CD1 LEU   301      -8.849  33.207  61.110  1.00 19.70           C
    ATOM   2445  CD2 LEU   301      -6.376  33.719  61.543  1.00 19.70           C
    ATOM   2446  C   LEU   301      -7.556  36.168  64.946  1.00 19.70           C
    ATOM   2447  O   LEU   301      -8.657  36.695  64.633  1.00 19.70           O
    ATOM   2448  OXT LEU   301      -6.821  36.580  65.884  1.00 19.70           O
    TER    2449      LEU   301
    HETATM 2450 NA    NA   302     -13.016  13.036  54.214  1.00 44.33          NA
    HETATM 2451  O   WAT   303     -18.411  13.587  59.094  1.00 27.41           O
    HETATM 2452  O   WAT   304     -11.894  17.279  58.575  1.00 18.35           O
    HETATM 2453  O   WAT   305     -15.811  12.728  54.157  1.00 39.81           O

I need to modify a line with the pattern OXT (see example below) in a third column and the next one from it (substitute it just with TERM) keeping the spacing between each of the lines as in the original doc.

    ATOM   2439  O   LEU   300      -4.699  34.599  65.335  1.00 83.23           O
    ATOM   2440  N   LEU   301      -6.822  33.898  65.057  1.00 19.70           N
    ATOM   2441  CA  LEU   301      -7.080  34.965  64.138  1.00 19.70           C
    ATOM   2442  CB  LEU   301      -8.165  34.630  63.101  1.00 19.70           C
    ATOM   2443  CG  LEU   301      -7.762  33.478  62.162  1.00 19.70           C
    ATOM   2444  CD1 LEU   301      -8.849  33.207  61.110  1.00 19.70           C
    ATOM   2445  CD2 LEU   301      -6.376  33.719  61.543  1.00 19.70           C
    ATOM   2446  C   LEU   301      -7.556  36.168  64.946  1.00 19.70           C
    ATOM   2447  O   LEU   301      -8.657  36.695  64.633  1.00 19.70           O
    ATOM   2448  N   NHE   301      -6.821  36.580  65.884  1.00 19.70           N
    TER
    HETATM 2450 NA    NA   302     -13.016  13.036  54.214  1.00 44.33          NA
    HETATM 2451  O   WAT   303     -18.411  13.587  59.094  1.00 27.41           O
    HETATM 2452  O   WAT   304     -11.894  17.279  58.575  1.00 18.35           O
    HETATM 2453  O   WAT   305     -15.811  12.728  54.157  1.00 39.81           O

I have tried to use

    awk '$3=="OXT"{ f=1; rn=NR; $3=$NF="N"; $4="NHE" }/TER/ && f && NR-rn == 1{ $0=$1 }1' file

It has produced right job but on a new string I had 1 space between each columns which is wrong.

    ATOM 2410 N NHE 299 -17.563 -15.711 -15.915 1.00 76.42 N

I need to keep the original format

    ATOM   2448  N   NHE   301      -6.821  36.580  65.884  1.00 19.70           N

Thanks for help!

J.

