[英](sed/awk) Extract values from text to csv file - even/odd lines pattern
I need to export some numeric values from a given ASCII text file and export it in a specific formatted csv file. 我需要从给定的ASCII文本文件中导出一些数字值,并将其导出到特定格式的csv文件中。 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. 我需要第5列的奇数行值和第2列的偶数行值。 They should be printed in a semicolon seperated csv file, with 10 values in each row.
它们应以分号分隔的csv文件打印,每行10个值。 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. 我从
awk '{print $5}'
和awk '{print $2}'
,但是我没有成功创建仅作用于偶数/奇数行的模式。
A simple way to do that? 一个简单的方法吗?
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: 以下脚本并没有使用
awk
的强大功能,但可以为您完成工作,并且可以理解:
NR % 2 { printf $5 ";" }
NR % 2 == 0 { printf $2 ";" }
NR % 10 == 0 { printf "\n" }
END { printf "\n" }
Usage (save the above as script.awk
): 用法(将上面的内容保存为
script.awk
):
awk -f script.awk input.txt
Something like this could work - 这样的事情可能会起作用-
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.
给定一个名为data.txt
的文件,请尝试:
awk '/SCF/{ printf $5 ";"; } /CCSD/{ printf($2); } NR % 10 == 0 { printf "\n"; }' data.txt
这可能对您有用:
tr -s ' ' ',' <file | paste -sd',\n' | cut -d, -f5,11 | paste -sd',,,,\n'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.