我有一个很大的文件,里面有很多不必要的东西,需要过滤掉。所以让我感兴趣的事情是这样的:
iteration # 20 ecut= 36.00 Ry beta=0.10
Davidson diagonalization with overlap
ethr = 4.07E-13, avg # of iterations = 3.8
total cpu time spent up to now is 351441.3 secs
End of self-consistent calculation
Number of k-points >= 100: set verbosity='high' to print the bands.
highest occupied, lowest unoccupied level (ev): 2.2896 4.1062好的,所以我需要计算: Bg=ELUMO−EHOMO,而ELUMO和EHOMO是最高和最低的占用率。问题是我想要这样的输出:
Iteration #<number>
Bg=xxx我的两个问题: 1.我可以通过“最高”字符串进行grep,所以我得到的每一行都是这样的:
highest occupied, lowest unoccupied level (ev): 2.3005 4.0791但是如何将变量设置为最高和最低的空闲级别呢?
2.由于并非每次迭代都会给出未占用级别的值(我想跳过它),我应该如何grep/find才能始终拥有迭代编号和未占用级别?
发布于 2020-03-30 20:30:23
使用awk
awk '/^iteration|highest/{if ($0 ~ "iteration") gsub(/ecut.*/ , "", $0); if ($0 ~ "highest") $0=($(NF-1)-$NF); print}' 演示:
$cat file.txt
iteration # 20 ecut= 36.00 Ry beta=0.10
Davidson diagonalization with overlap
ethr = 4.07E-13, avg # of iterations = 3.8
total cpu time spent up to now is 351441.3 secs
End of self-consistent calculation
Number of k-points >= 100: set verbosity='high' to print the bands.
highest occupied, lowest unoccupied level (ev): 2.2896 4.1062
$awk '/^iteration|highest/{if ($0~"iteration") gsub(/ecut.*/,"",$0); if ($0 ~ "highest") $0=($(NF-1)-$NF); print}' < file.txt
iteration # 20
-1.8166
$解释:
/^iteration|highest/ -- Select only rows starting with iteration or with highesh
($0 ~ "iteration") -- $0 means entire row, Check if row have iteration pattern
gsub(/ecut.*/ , "", $0) -- Delete all char after **ecut**
NF -- > number of fields in row
$NF --> last field
$(NF-1) --> second last field
$0=($(NF-1)-$NF -- Set current row as second last field - last fieldhttps://stackoverflow.com/questions/60926664
复制相似问题