简体   繁体   English

Linux BASH,如何从另一个文件添加两行

[英]Linux BASH, how to add two lines from another file

Working on Ubuntu, using bash.在 Ubuntu 上工作,使用 bash。

I have two files.我有两个文件。 File 1 contains a list of atoms and their positions in space, and looks like this:文件 1 包含一个原子列表及其在空间中的位置,如下所示:

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.文件 2 非常相似,但包含更多信息。 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).我的问题是:文件 1 具有正确的坐标,但没有所需的其余信息(速度和力)。 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:如何从每个原子(第 3&4、7&8、11&12 等)中取出两条线并将它们插入相应原子的坐标下方,以便输出如下所示:

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.我的方法是将两个想要的文件与文件 2 分开,然后尝试将它们添加到第一个文件中的坐标下方。 Unfortunately I have not found a way to do this.不幸的是,我还没有找到一种方法来做到这一点。

Thank you.谢谢你。

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 FNR==NR 检查我们是否在第一个文件中

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). ($0 in a) 检查数组中是否存在标题行(文件 1 中的原子 = 文件 2 中的原子)。

yes?是的? print all the stuff :)打印所有的东西:)

Input: file1输入:文件 1

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

file2文件 2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM