首页 > 解决方案 > 如何从一个文件中 grep 数据,进行小操作,然后将它们附加到另一个文件中

问题描述

我有两个文件 一个是(case1):

Automatically generated mesh
      10
Reciprocal lattice
    0.00000000000000    0.00000000000000    0.00000000000000             1
    0.20000000000000   -0.00000000000000   -0.00000000000000             8
    0.40000000000000   -0.00000000000000   -0.00000000000000             8
    0.20000000000000    0.20000000000000   -0.00000000000000             6
    0.40000000000000    0.20000000000000   -0.00000000000000            24
   -0.40000000000000    0.20000000000000    0.00000000000000            24
   -0.20000000000000    0.20000000000000    0.00000000000000            12
    0.40000000000000    0.40000000000000   -0.00000000000000             6
   -0.40000000000000    0.40000000000000    0.00000000000000            12
   -0.40000000000000    0.40000000000000    0.20000000000000            24

另一个是(case2)

   0.06309051 -0.03237807  0.05437503       0.017
   0.06309051 -0.03642533  0.05151319       0.017

 k-points in reciprocal lattice and weights: K-Path Generated.
   0.00000000  0.00000000  0.00000000       0.017
   0.05555556  0.00000000  0.05555556       0.017
   0.11111111  0.00000000  0.11111111       0.017
   0.16666667  0.00000000  0.16666667       0.017
   0.22222222  0.00000000  0.22222222       0.017
   0.27777778  0.00000000  0.27777778       0.017
   0.33333333  0.00000000  0.33333333       0.017
   0.38888889  0.00000000  0.38888889       0.017
   0.44444444  0.00000000  0.44444444       0.017
   0.50000000  0.00000000  0.50000000       0.017
   0.50000000  0.00000000  0.50000000       0.017
   0.51388889  0.02777778  0.51388889       0.017
   0.52777778  0.05555556  0.52777778       0.017
   0.54166667  0.08333333  0.54166667       0.017
   0.55555556  0.11111111  0.55555556       0.017
   0.50000000  0.02777778  0.52777778       0.017
   0.50000000  0.00000000  0.50000000       0.017

 position of ions in fractional coordinates (direct lattice)

我需要

  1. case2 文件中grepk-points in reciprocal lattice and weights: K-Path Generated.和之后的第一个空行之间的数据并制作最后一列(i.e. before the row having position of ions in fractional coordinates (direct lattice))0
  2. 计算case1 文件之后Reciprocal lattice的总行数和步骤 1 中 grepped 文件中的总行数,然后使用case1 文件第二行中可用的数字更新此数字(这里是 10,但可能会有所不同
  3. 将第一步中的 grepped 数据附加到文件 1st 中。并将第一个文件复制为 KPOINTS

最终的KPOINTS文件应为

Automatically generated mesh
      27
Reciprocal lattice
    0.00000000000000    0.00000000000000    0.00000000000000             1
    0.20000000000000   -0.00000000000000   -0.00000000000000             8
    0.40000000000000   -0.00000000000000   -0.00000000000000             8
    0.20000000000000    0.20000000000000   -0.00000000000000             6
    0.40000000000000    0.20000000000000   -0.00000000000000            24
   -0.40000000000000    0.20000000000000    0.00000000000000            24
   -0.20000000000000    0.20000000000000    0.00000000000000            12
    0.40000000000000    0.40000000000000   -0.00000000000000             6
   -0.40000000000000    0.40000000000000    0.00000000000000            12
   -0.40000000000000    0.40000000000000    0.20000000000000            24
   0.00000000  0.00000000  0.00000000       0
   0.05555556  0.00000000  0.05555556       0
   0.11111111  0.00000000  0.11111111       0
   0.16666667  0.00000000  0.16666667       0
   0.22222222  0.00000000  0.22222222       0
   0.27777778  0.00000000  0.27777778       0
   0.33333333  0.00000000  0.33333333       0
   0.38888889  0.00000000  0.38888889       0
   0.44444444  0.00000000  0.44444444       0
   0.50000000  0.00000000  0.50000000       0
   0.50000000  0.00000000  0.50000000       0
   0.51388889  0.02777778  0.51388889       0
   0.52777778  0.05555556  0.52777778       0
   0.54166667  0.08333333  0.54166667       0
   0.55555556  0.11111111  0.55555556       0
   0.50000000  0.02777778  0.52777778       0
   0.50000000  0.00000000  0.50000000       0

标签: awksedgrep

解决方案


如果您继续添加到我的原始评论中,您可以构建一个简短的awk脚本来为您处理整个转换。你基本上有两套规则。我们将首先读取的文件的规则case2,您可以在哪里比较FNR(文件记录数)等于(记录数),NR这意味着您正在读取第一个文件。

对于要读取的第二个文件case1NR继续增加,因此您的 currentFNR不再等于总数NR- 这是在同一脚本中以不同方式处理每个文件的便捷方法。

对于case2case1文件,您可以执行以下操作:

awk '
    NR==FNR && /k-points/ { c2=1; next }
    NR==FNR && !NF { m=c2; c2=0 }
    NR==FNR && c2  { sub($NF,"0"); b[c2++] = $0 }
    
    NR!=FNR && /Reciprocal/ { n=1; next } 
    NR!=FNR && n  { a[n++] = $0 }
END {
    print "Automatically generated mesh"
    print "       " m + n - 2
    print "Reciprocal lattice"
    for (i=1; i<n; i++)
        print a[i]
    for (i=1; i<m; i++)
        print b[i]
}' case2 case1

示例使用/输出

您只需选择上面的代码,然后用鼠标中键将其粘贴到 xterm 中,当前目录包含两个文件case2case1进行测试:

$ awk '
>     NR==FNR && /k-points/ { c2=1; next }
>     NR==FNR && !NF { m=c2; c2=0 }
>     NR==FNR && c2  { sub($NF,"0"); b[c2++] = $0 }
>
>     NR!=FNR && /Reciprocal/ { n=1; next }
>     NR!=FNR && n  { a[n++] = $0 }
> END {
>     print "Automatically generated mesh"
>     print "       " m + n - 2
>     print "Reciprocal lattice"
>     for (i=1; i<n; i++)
>         print a[i]
>     for (i=1; i<m; i++)
>         print b[i]
> }' case2 case1
Automatically generated mesh
       27
Reciprocal lattice
    0.00000000000000    0.00000000000000    0.00000000000000             1
    0.20000000000000   -0.00000000000000   -0.00000000000000             8
    0.40000000000000   -0.00000000000000   -0.00000000000000             8
    0.20000000000000    0.20000000000000   -0.00000000000000             6
    0.40000000000000    0.20000000000000   -0.00000000000000            24
   -0.40000000000000    0.20000000000000    0.00000000000000            24
   -0.20000000000000    0.20000000000000    0.00000000000000            12
    0.40000000000000    0.40000000000000   -0.00000000000000             6
   -0.40000000000000    0.40000000000000    0.00000000000000            12
   -0.40000000000000    0.40000000000000    0.20000000000000            24
   0.00000000  0.00000000  0.00000000       0
   0.05555556  0.00000000  0.05555556       0
   0.11111111  0.00000000  0.11111111       0
   0.16666667  0.00000000  0.16666667       0
   0.22222222  0.00000000  0.22222222       0
   0.27777778  0.00000000  0.27777778       0
   0.33333333  0.00000000  0.33333333       0
   0.38888889  0.00000000  0.38888889       0
   0.44444444  0.00000000  0.44444444       0
   0.50000000  0.00000000  0.50000000       0
   0.50000000  0.00000000  0.50000000       0
   0.51388889  0.02777778  0.51388889       0
   0.52777778  0.05555556  0.52777778       0
   0.54166667  0.08333333  0.54166667       0
   0.55555556  0.11111111  0.55555556       0
   0.50000000  0.02777778  0.52777778       0
   0.50000000  0.00000000  0.50000000       0

如果您还有其他问题,请告诉我。


推荐阅读