【问题标题】:search keywords in a folder and split output using the same keys在文件夹中搜索关键字并使用相同的键拆分输出
【发布时间】:2020-10-05 03:36:49
【问题描述】:

我需要在 Linux 上搜索这些关键字

  • 渲染完成
  • 在
  • 处渲染图像
  • CPU @

包含在一个包含多个 .txt 文件的文件夹中。

使用grep 我获得了以下 3 个不同行中列出的输出

1 x Intel(R) Core(TM) i9-7920X CPU @ 2.90GHz (12 cores, 24 logical) with 63929MB
rendering image at 640 x 480, 1 AA sample
render done in 1:17.135

我现在需要的是使用相同的关键字(render done inrendering image atCPU @)将其拆分,以便输出看起来像 render done in1:17.135 rendering image at640 x 480 CPU @2.90GHz

在实践中,我需要通过其描述分隔值,因此 in 可以轻松指向它并进行进一步计算(在 excel 或类似中)。 如果输出是 .xls 或 .csv 文件,那么如果这些字段包含在不同的单元格中,那就太好了。

我目前正在使用这个命令

grep -E -r 'render done in|rendering image at|CPU @' /home/ciara/Documents/Logs/ > /home/ciara/Documents/output.txt

我尝试与cut 或awk 分开,但没有成功。 有什么帮助吗?

【问题讨论】:

    标签: linux grep


    【解决方案1】:

    由于您需要对项目重新排序,因此您需要使用某种可以轻松完成的语言。例如,perl、python、awk。这是在 awk 中:

    /CPU @/ {
       freq=$8
       getline
       res=$4" x "substr($6,1,length($6)-1)
       getline
       t=$4
       print "render done in " t " rendering image at " res " CPU @ " freq
       next
    }
    

    您可以轻松调整打印语句以在此处添加逗号并将结果用作 csv。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2015-03-18
      • 1970-01-01
      • 2021-04-17
      • 2018-02-21
      相关资源
      最近更新 更多