如何解决如何从一个文件中提取数据,做一些小操作,然后将它们附加到另一个文件中
我有两个文件 一个是(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)
我需要
- 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))
之后的第一个空行之间的数据 case2 文件并使最后一列0
- 计算
Reciprocal lattice
之后case1文件中的总行数以及在步骤1中grepped的文件中的总行数,然后 使用 case1 的第二行中可用的数字更新此数字 文件(这里是 10 个,但可能会有所不同) - 将第 1 步的 grepped 数据附加到 file 1st. 并将第 1 个文件复制为 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
继续递增,因此您当前的 FNR
不再等于总 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
如果您还有其他问题,请告诉我。
,使用 GNU awk:
awk 'FNR==NR && /^Reciprocal lattice/ { scnt=1;next } FNR==NR && scnt==1 { cnt++;map1[FNR]=$0 } NR != FNR && /^$/ { scnt1=0 } FNR != NR && / k-points in reciprocal lattice and weights: K-Path Generated./ { scnt1=1;next } FNR != NR && scnt1==1 { cnt++;gsub($4,"0",$4);map[FNR]=$0 } END { PROCINFO["sorted_in"]="@ind_num_asc";print "Automatically generated mesh";printf "\t%s\n",cnt;print "Reciprical lattice";for (i in map1) { print map1[i] } for (i in map) { print map[i] } }' case1 case2
说明:
awk 'FNR==NR && /^Reciprocal lattice/ { # Process the first file (FNR==NR). Where the line start with "Reciprocal lattice" set a marker variable scnt to 1
scnt=1;
next
}
FNR==NR && scnt==1 { # Where it is the first file and marker scnt is 1,process
cnt++; # Increment a total line counter
map1[FNR]=$0 # Set up a map array with the file record number the index and the line ($0) the value
}
NR != FNR && /^$/ { # Process where we encounter the second file (NR != FNR) and the line is blank
scnt1=0 # Set second tracking variable scnt1 to 0
}
FNR != NR && / k-points in reciprocal lattice and weights: K-Path Generated./ {
scnt1=1; # Where it is the second file and we see the text "k-point ...",set second tracking variable scnt1 to 1
next
}
FNR != NR && scnt1==1 { # Process where second file and second tracking variable is 1
cnt++; # Increment total line count
gsub($4,$4); # Use gsub to replace the 4th field with 0
map[FNR]=$0 # Set up another array map with the file number record as the index and the amended line as the value
}
END { # Process at the end of processing both files.
PROCINFO["sorted_in"]="@ind_num_asc"; # Set the array ordering
print "Automatically generated mesh";
printf "\t%s\n",cnt;print "Reciprical lattice"; # Print text and total
for (i in map1) {
print map1[i] # Loop through map1 and print lines
}
for (i in map) {
print map[i] # Loop through map and print lines
}
}' case1 case2
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。