(sed/awk) 从文本中提取值到 csv 文件 - 偶数/奇数行模式

发布于 2024-12-29 00:24:13 字数 747 浏览 0 评论 0原文

我需要从给定的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿看小鸭子会跑 2025-01-05 00:24:13

以下脚本并未使用 awk 的很多强大功能,但可以为您完成这项工作,并且希望易于理解:

NR % 2 { printf "%s;", $5 }
NR % 2 == 0 { printf "%s;", $2 }
NR % 10 == 0 { print "" }
END { print "" }

用法(将以上内容另存为 script.awk):

awk -f script.awk input.txt

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:

NR % 2 { printf "%s;", $5 }
NR % 2 == 0 { printf "%s;", $2 }
NR % 10 == 0 { print "" }
END { print "" }

Usage (save the above as script.awk):

awk -f script.awk input.txt
黄昏下泛黄的笔记 2025-01-05 00:24:13

给定一个名为 data.txt 的文件,请尝试:

awk '/SCF/{ printf $5 ";"; } /CCSD/{ printf($2); } NR % 10 == 0 { printf "\n"; }' data.txt

Given a file called data.txt, try:

awk '/SCF/{ printf $5 ";"; } /CCSD/{ printf($2); } NR % 10 == 0 { printf "\n"; }' data.txt
狠疯拽 2025-01-05 00:24:13

像这样的东西可以工作 -

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.

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.
阿楠 2025-01-05 00:24:13

这可能对你有用:

 tr -s ' ' ',' <file | paste -sd',\n' | cut -d, -f5,11 | paste -sd',,,,\n'

This might work for you:

 tr -s ' ' ',' <file | paste -sd',\n' | cut -d, -f5,11 | paste -sd',,,,\n'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文