如何使用grep或awk以这种模式提取数据?

如何使用grep或awk以这种模式提取数据?,awk,grep,Awk,Grep,我的文档中有以下模式的多个实例: Dipole Moment: [D] X: 1.5279 Y: 0.1415 Z: 0.1694 Total: 1.5438 我想求出总偶极矩,1.5438。我怎样才能做到这一点 当我输入“偶极矩:[D]”文件名时,我没有得到后面的行。我不熟悉这些命令行界面。如果您能提供任何帮助,我们将不胜感激。请尝试以下内容。使用GNUawk中显示的样本编写和测试 awk '/Dipole Moment:

我的文档中有以下模式的多个实例:

Dipole Moment: [D]
     X:     1.5279      Y:     0.1415      Z:     0.1694     Total:     1.5438
我想求出总偶极矩,1.5438。我怎样才能做到这一点


当我输入“偶极矩:[D]”文件名时,我没有得到后面的行。我不熟悉这些命令行界面。如果您能提供任何帮助,我们将不胜感激。

请尝试以下内容。使用GNU
awk
中显示的样本编写和测试

awk '/Dipole Moment: \[D\]/{found=1;next} found{print $NF;found=""}' Input_file
说明:添加上述内容的详细说明

awk '                     ##Starting awk program from here.
/Dipole Moment: \[D\]/{   ##Checking if line contains Dipole Moment: \[D\] escaped [ and ] here.
  found=1                 ##Setting found to 1 here.
  next                    ##next will skip all further statements from here.
}
found{                    ##Checking condition if found is NOT NULL then do following.
  print $NF               ##Printing last field of current line here.
  found=""                ##Nullifying found here.
}
' Input_file              ##Mentioning Input_file name here.
Sed备选方案:

sed -rn '/^Dipole/{n;s/(^[[:space:]]{5}.*[[:space:]]{5})(.*)(([[:space:]]{5}.*+[:][[:space:]]{5}.*){3})/\2/p}' file
搜索以“偶极子”开头的行,然后读取下一行。根据正则表达式将该行拆分为三个部分,并仅用该行替换第二部分,打印结果