我需要从给定的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
我需要第5列中的奇数行值和偶数行的第2列值。它们应以分号分隔的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}'
开始,但是我没有成功创建一个只对偶数/奇数行作用的模式。
一种简单的方法吗?
答案 0 :(得分:3)
以下脚本不会使用awk
的强大功能,但会为您完成这项工作,并且希望可以理解:
NR % 2 { printf $5 ";" }
NR % 2 == 0 { printf $2 ";" }
NR % 10 == 0 { printf "\n" }
END { printf "\n" }
用法(将上述内容保存为script.awk
):
awk -f script.awk input.txt
答案 1 :(得分:1)
这样的事情可以起作用 -
awk '{x = NF > 3 ? $5 : $2 ; printf("%s;",x)}(NR % 10 == 0){print OFS}' file
|_____________________| |________| |___________||_________|
| | | |
This is a `ternary operator`, Print with `NR` is a `OFS` is another built-in
what it does is checks the line formatting a built-in that has a default value of
for number of fields (`NF`). If to add that keeps `\n`
the number of fields is more than a ";" track of
3, we assign $5 value to variable x number of lines.
else we assign $2 value We are using modulo
operator to check when
10 lines are crossed.
答案 2 :(得分:1)
鉴于名为data.txt
的文件,请尝试:
awk '/SCF/{ printf $5 ";"; } /CCSD/{ printf($2); } NR % 10 == 0 { printf "\n"; }' data.txt
答案 3 :(得分:0)
这可能对您有用:
tr -s ' ' ',' <file | paste -sd',\n' | cut -d, -f5,11 | paste -sd',,,,\n'