Quantcast
Channel: Active questions tagged bash+awk - Ask Ubuntu
Viewing all articles
Browse latest Browse all 102

Keep original spacing while doing replacements [AWK, SED, GREP, ...]

$
0
0

I have two files which content are:

File 1:

ATOM      1  N   LEU     1     -10.186 -21.276  -0.497  1.00  0.00      PROTATOM      2  HT1 LEU     1     -10.773 -22.005  -0.843  1.00  0.00      PROTATOM      3  HT2 LEU     1      -9.316 -21.441  -0.958  1.00  0.00      PROTATOM      4  HT3 LEU     1     -10.007 -21.389   0.478  1.00  0.00      PROTATOM      5  CA  LEU     1     -10.642 -19.924  -0.729  1.00  0.00      PROTATOM      6  HA  LEU     1     -10.092 -19.286  -0.214  1.00  1.00      PROTATOM      7  CB  LEU     1     -10.604 -19.551  -2.214  1.00  1.00      PROTATOM      8  HB1 LEU     1     -11.432 -19.802  -2.731  1.00  1.00      PROTATOM      9  HB2 LEU     1      -9.764 -19.989  -2.566  1.00  1.00      PROTATOM     10  CG  LEU     1     -10.447 -17.969  -2.340  1.00  1.00      PROT

File 2:

ATOM      1  N   LEU     1     -10.186 -21.276  -0.497  1.00  2.00      PROTATOM      2  HT1 LEU     1     -10.773 -22.005  -0.843  1.00  4.00      PROTATOM      3  HT2 LEU     1      -9.316 -21.441  -0.958  5.00  4.00      PROTATOM      4  HT3 LEU     1     -10.007 -21.389   0.478  1.00  4.00      PROTATOM      5  CA  LEU     1     -10.642 -19.924  -0.729  1.00  3.00      PROTATOM      6  HA  LEU     1     -10.092 -19.286  -0.214  1.00  4.00      PROTATOM      7  CB  LEU     1     -10.604 -19.551  -2.214  1.00  3.00      PROTATOM      8  HB1 LEU     1     -11.432 -19.802  -2.731  1.00  4.00      PROTATOM      9  HB2 LEU     1      -9.764 -19.989  -2.566  1.00  4.00      PROTATOM     10  CG  LEUUUU  1     -10.447 -17.969  -2.340  1.00  3.00      PROT

The desired output file would be:

ATOM      1  N   LEU     1     -10.186 -21.276  -0.497  1.00  0.00      PROTATOM      2  HT1 LEU     1     -10.773 -22.005  -0.843  1.00  0.00      PROTATOM      3  HT2 LEU     1      -9.316 -21.441  -0.958  5.00  0.00      PROTATOM      4  HT3 LEU     1     -10.007 -21.389   0.478  1.00  0.00      PROTATOM      5  CA  LEU     1     -10.642 -19.924  -0.729  1.00  0.00      PROTATOM      6  HA  LEU     1     -10.092 -19.286  -0.214  1.00  1.00      PROTATOM      7  CB  LEU     1     -10.604 -19.551  -2.214  1.00  1.00      PROTATOM      8  HB1 LEU     1     -11.432 -19.802  -2.731  1.00  1.00      PROTATOM      9  HB2 LEU     1      -9.764 -19.989  -2.566  1.00  1.00      PROTATOM     10  CG  LEUUUU  1     -10.447 -17.969  -2.340  1.00  1.00      PROT

I want to replace the content of the 10th column of file 2 with the content of 10th column in file 1

My first try was:

awk -v OFS="\t" 'FNR==NR{a[NR]=$10;next}{$10=a[FNR]}1' file1 file2

But the tabbed file that I get, the program that I am using to read it, it does not read it in tabbed form.

Then, I tried:

awk 'FNR==NR{a[NR]=$10;next}{$10=a[FNR]}1' file1 file2

And the program does not read it neither.

So... I need to keep the original spacing.

Any suggestion?


Viewing all articles
Browse latest Browse all 102

Trending Articles