(sed/awk) 从文本中提取值到 csv 文件 - 偶数/奇数行模式
我需要从给定的 ASCII 文本文件中导出一些数值,并将其导出到特定格式的 csv 文件中。输入文件具有偶数/奇数行模式:
SCF Done: E(UHF) = -216.432419652 A.U. after 12 cycles
CCSD(T)= -0.21667965032D+03
SCF Done: E(UHF) = -213.594303492 A.U. after 10 cycles
CCSD(T)= -0.21379841974D+03
SCF Done: E(UHF) = -2.86120139864 A.U. after 6 cycles
CCSD(T)= -0.29007031339D+01
and so on
我需要第五列中的奇数行值和偶数行第二列值。它们应打印在以分号分隔的 csv 文件中,每行 10 个值。因此,输出应该看起来像
-216.432419652;-0.21667965032D+03;-213.594303492;-0.21379841974D+03;-2.86120139864;-0.29007031339D+01; ...linebreak after 5 pairs of values
我从 awk '{print $5}'
和 awk '{print $2}'
开始,但是我没有成功创建一个模式,该模式只是作用于偶数/奇数线上。
有一个简单的方法吗?
I need to export some numeric values from a given ASCII text file and export it in a specific formatted csv file. The input file has got the even / odd line pattern:
SCF Done: E(UHF) = -216.432419652 A.U. after 12 cycles
CCSD(T)= -0.21667965032D+03
SCF Done: E(UHF) = -213.594303492 A.U. after 10 cycles
CCSD(T)= -0.21379841974D+03
SCF Done: E(UHF) = -2.86120139864 A.U. after 6 cycles
CCSD(T)= -0.29007031339D+01
and so on
I need the odd line value in the 5th column and the even line 2nd column value. They should be printed in a semicolon seperated csv file, with 10 values in each row. So the output should look like
-216.432419652;-0.21667965032D+03;-213.594303492;-0.21379841974D+03;-2.86120139864;-0.29007031339D+01; ...linebreak after 5 pairs of values
I started with awk '{print $5}'
and awk '{print $2}'
, however I was not successful in creating a pattern that just acts on even/odd lines.
A simple way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下脚本并未使用
awk
的很多强大功能,但可以为您完成这项工作,并且希望易于理解:用法(将以上内容另存为
script.awk):
The following script doesn't use a lot of the great power of
awk
, but will do the job for you and is hopefully understandable:Usage (save the above as
script.awk
):给定一个名为
data.txt
的文件,请尝试:Given a file called
data.txt
, try:像这样的东西可以工作 -
Something like this could work -
这可能对你有用:
This might work for you: