[英]How to extract data in such a pattern using grep or awk?
我的文檔中有以下模式的多個實例:
Dipole Moment: [D]
X: 1.5279 Y: 0.1415 Z: 0.1694 Total: 1.5438
我想提取總偶極矩,所以 1.5438。 我怎樣才能做到這一點?
當我輸入grep "Dipole Moment: [D]" filename
時,我沒有得到后面的行。 我是這些命令行界面的新手。 您能提供的任何幫助將不勝感激。
請您嘗試以下操作。 使用 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
搜索以“Dipole”開頭的行,然后閱讀下一行。 根據正則表達式將此行拆分為三個部分,並僅將該行替換為第二部分,打印結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.