Help!!!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help!!!
# 1  
Old 11-26-2010
Replace columns in different files.

Hi all!

I have file1 which looks like this:

Code:
 Nº de átomos totales
          157
 Información atómica individual
NA      RX(A)             RY(A)            RZ(A)           EPSILON(eV)  SIGMA(A) Q (e)  FVX  FVY  FVZ     MOL
79    12.4923970000     0.0000000000     0.0000000000    0.00150      3.217    0.0000    0    0    0        0
79     7.4954386000     0.0000000000     0.0000000000    0.00150      3.217    0.0000    0    0    0        0
79     9.9939184000     1.4424978000     0.0000000000    0.00150      3.217    0.0000    0    0    0        0
79    12.4923970000     2.8849957000     0.0000000000    0.00150      3.217    0.0000    0    0    0        0
79     2.4984796000     0.0000000000     0.0000000000    0.00150      3.217    0.0000    0    0    0        0
79     4.9969592000     1.4424978000     0.0000000000    0.00150      3.217    0.0000    0    0    0        0
7      9.9940004349     10.0970001221     10.0670000000  0.02250      3.660    0.0000    1    1    1        1
6      9.9687515010     10.9408269779     11.2463665363  0.00512      3.905    0.0000    1    1    1        1
6      9.9946844294     10.0741408239     12.4876889268  0.00512      3.905    0.0000    1    1    1        1
.
.
(the file is longer)

And file2 which look like:

Code:
Au   12.90881    0.72125   87.57815
Au    7.91185    0.72125   87.57815
Au   10.41033    2.16375   87.57815
Au   12.90881    3.60625   87.57815
Au    2.91489    0.72125   87.57815
Au    5.41337    2.16375   87.57815
N    10.41104   10.81936   96.97897
C    11.10785   11.56856   98.02487
C    10.28173   11.54696   99.31489
.
.

What I want to do is replace columns $2 $3 and $4 from file2 into therespective columns of file1. So the final result would look like:

Code:
 Nº de átomos totales
          157
 Información atómica individual
NA      RX(A)             RY(A)            RZ(A)           EPSILON(eV)  SIGMA(A) Q (e)  FVX  FVY  FVZ     MOL
79  12.90881    0.72125   87.57815  0.00150     3.217     0.0000    0    0    0       0
79   7.91185    0.72125   87.57815  0.00150     3.217     0.0000    0    0    0       0
79  10.41033    2.16375   87.57815  0.00150     3.217     0.0000    0    0    0       0
79  12.90881    3.60625   87.57815  0.00150     3.217     0.0000    0    0    0       0
79   2.91489    0.72125   87.57815  0.00150     3.217     0.0000    0    0    0       0
79   5.41337    2.16375   87.57815  0.00150     3.217     0.0000    0    0    0       0
7   10.41104   10.81936   96.97897  0.02250     3.660     0.0000    1    1    1       1
6   11.10785   11.56856   98.02487  0.00512     3.905     0.0000    1    1    1       1
6   10.28173   11.54696   99.31489  0.00512     3.905     0.0000    1    1    1       1
.
.(the file is longer)

I hope I made my point clear and solve my problem.
Thanks a lot!!

Last edited by ezitoc; 11-27-2010 at 09:55 AM.. Reason: I have received a warning from forum
# 2  
Old 11-26-2010
I am sure there are more elegant solution to do it but i'm too tired to stress my brain Smilie... so here is a simple way
Code:
head -4 file1 >output
tail +5 file1 | awk '{print$1}' >f1.tmp
awk '{print$2,$3,$4}' file2 >f2.tmp
tail +5 file1 | awk '{print$5,$6,$7,$8,$9,$10,$11}' >f3.tmp
paste f1.tmp f2.tmp f3.tmp >>output
rm f1.tmp f2.tmp f3.tmp

If you don't have the tail +5 , check if you have tail -n +5
# 3  
Old 11-26-2010
Hi,

Using 'awk'.
Code:
$ awk --version | sed '1q'
GNU Awk 3.1.8
$ cat script.awk
BEGIN {
    i = 0;
    j = 0;
}

## File 2.
FNR == NR {
    c1[i] = $2;
    c2[i] = $3;
    c3[i] = $4;
    ++i;
    next;
}

## File 1.
NR > FNR && $1 ~ /[[:digit:]]+/ && NF == 11 {
    $2 = c1[j];
    $3 = c2[j];
    $4 = c3[j];
    ++j;
    $0 = $0;
}

{ 
    print;
}
$ awk -f script.awk file2 file1
$ ## (Output supressed).

Regards,
Birei
# 4  
Old 11-27-2010
Thanks so much for such a quick reply. I will test this when I return to work. Thanks!!

Ezequiel
# 5  
Old 11-27-2010
Code:
awk 'NR>4{getline x<f; split(x,F); for(i=2;i<=4;i++)$i=sprintf("%9.5f",F[i])}1' OFS="\t" f=file2 file1

 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question