Bdrs
Bdrs

Reputation: 143

Linux BASH, how to add two lines from another file

Working on Ubuntu, using bash.

I have two files. File 1 contains a list of atoms and their positions in space, and looks like this:

Si               1        14
  24.094049488113697  22.249517320000000  5.4091803780000000
Si               2        14
 -21.980209241886303  23.466150130000000 -6.4407518510000000
Si               3        14
 -9.8193586518863060 -13.586795180000000 -14.608877780000000

This file goes on until all the atoms are described.

File 2 is quite similar, but contains more information. In addition to names and locations, it contains information about velocities and forces. It looks like this:

Si               1
     22.31756370         22.24951732         5.409180378    
   29.0968650481      -12.2276780157      -7.08186598428    
   30498.6028163      -9406.07172249      -27393.4141429    
Si               2
    -23.75669503         23.46615013        -6.440751851    
  -28.7812217378       31.2316292200      -34.6050775946    
   40272.8675096      -40472.1160399      -1103.73416448    
Si               3
    -11.59584444        -13.58679518        -14.60887778    
   13.5323597131       42.5618815724       8.79048959706    
   6758.60998012      -9418.56231552       3386.31657511

The problem I have is this: File 1 has the correct coordinates, but does not have the rest of the information, which is needed (velocities and forces). How can I take the two lines from every atom (lines 3&4, 7&8, 11&12 etc.) and insert them below the coordinates of the corresponding atoms, so that the output looks like this:

Si               1        14
  24.094049488113697  22.249517320000000  5.4091803780000000
  29.0968650481      -12.2276780157      -7.08186598428
  30498.6028163      -9406.07172249      -27393.4141429    
Si               2        14
 -21.980209241886303  23.466150130000000 -6.4407518510000000
 -28.7812217378       31.2316292200      -34.6050775946    
  40272.8675096      -40472.1160399      -1103.73416448
Si               3        14
 -9.8193586518863060 -13.586795180000000 -14.608877780000000
  13.5323597131       42.5618815724       8.79048959706    
  6758.60998012      -9418.56231552       3386.31657511

(The coordinates are from the first file, but the velocities and forces are from the second one)

My approach was to separate the two wanted files from file 2 and then try to add them below the coordinates in the first file. Unfortunately I have not found a way to do this.

Thank you.

Upvotes: 2

Views: 60

Answers (1)

Daniel
Daniel

Reputation: 783

awk 'FNR==NR { getline a[$1$2]; next; } ($1$2 in a){ print $0; print a[$1$2]; getline; getline; print; getline; print }' file1 file2

Explanation:

FNR==NR checks if we are in the first file

yes? Save the next line in an array

no? We are in the second file.

($0 in a) Check if the header line exists in the array (atom in file1 = atom in file2).

yes? print all the stuff :)

Input: file1

Si  1   14
24  22  5
Si  2   14
21  23  6
Si  3   14
9   13  14

file2

Si  1   
x   x   x
y   y   y   
z   z   z
Si  2   
x   x   x
y   y   y
z   z   z
Si  3   
x   x   x
y   y   y
z   z   z

Output:

Si  1   14
24  22  5
y   y   y
z   z   z   
Si  2   14
21  23  6
y   y   y
z   z   z
Si  3   14
9   13  14
y   y   y
z   z   z

Upvotes: 1

Related Questions