awk - 如何从一个文件中 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)
我需要
- 从
case2 文件中grep
k-points in reciprocal lattice and weights: K-Path Generated.
和之后的第一个空行之间的数据并制作最后一列(i.e. before the row having position of ions in fractional coordinates (direct lattice))
0
- 计算case1 文件之后
Reciprocal lattice
的总行数和步骤 1 中 grepped 文件中的总行数,然后使用case1 文件第二行中可用的数字更新此数字(这里是 10,但可能会有所不同) - 将第一步中的 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
解决方案
如果您继续添加到我的原始评论中,您可以构建一个简短的awk
脚本来为您处理整个转换。你基本上有两套规则。我们将首先读取的文件的规则case2
,您可以在哪里比较FNR
(文件记录数)等于(记录数),NR
这意味着您正在读取第一个文件。
对于要读取的第二个文件case1
,NR
继续增加,因此您的 currentFNR
不再等于总数NR
- 这是在同一脚本中以不同方式处理每个文件的便捷方法。
对于case2
和case1
文件,您可以执行以下操作:
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 中,当前目录包含两个文件case2
并case1
进行测试:
$ 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
如果您还有其他问题,请告诉我。
推荐阅读
- wpf - 如何使用样式的触发器但覆盖行为
- nfc - ACR1252 Felica 仿真
- javascript - 监听 mongoDB 中的更新操作并自动更新某些字段
- javascript - 查找数组数组中的第一个元素,第二个元素具有最高值
- r - 是否可以在 R 中使用来自 data.table 的 fwrite 来编写一个带有 sep="" 的表
- java - 用于扑克的 Java 蒙特卡罗模拟
- android - Flutter:未知的操作系统。无法安装 Dart SDK
- javascript - 完整日历不会显示取自 AJAX 的事件
- python - 如何将 pandas DataFrame 插入 Microsoft SQL Server 表?
- c - C - 昨天的日期