gpt4 book ai didi

linux - grep --output-only 到单行

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:32 25 4
gpt4 key购买 nike

我目前正在运行一个非常接近我想要的 grep:

$grep ! myinput_file/* | grep  -Po '\d+\.\d+|\d+'

我喜欢这个命令只返回行的匹配内容,但希望输出是逐行的。例如。当我当前运行命令时,我得到:

7.969
80
2
152.03886767
7.969
80
4
152.10112473
7.969
80
6
152.10200398
7.969
80
8
152.10203172

我希望获得以下形式的输出:

7.969 80 2 152.03886767
7.969 80 4 152.10112473
7.969 80 6 152.10200398
7.969 80 8 152.10203172

我可以编写脚本或在输出上使用 vim,但我怀疑有更优雅的解决方案...

PS源文件看起来像:

$grep ! encutkpoint_calculations/* | grep  -P '\d+\.\d+|\d+'

encutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=2.out:! total energy = -152.03886767 Ry
encutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=4.out:! total energy = -152.10112473 Ry
encutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=6.out:! total energy = -152.10200398 Ry
encutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=8.out:! total energy = -152.10203172 Ry

最佳答案

由于数据也是从文件名中提取的,所以我将按原样保留第一次使用 grep

$ # this won't depend on knowing how many matches are found per line
$ # this would also work if there are different number of matches per line
$ grep '!' encutkpoint_calculations/* | perl -lne 'print join " ", /\d+(?:\.\d+)?/g'
7.969 80 2 152.03886767
7.969 80 4 152.10112473
7.969 80 6 152.10200398
7.969 80 8 152.10203172

替代方法是对数据进行后处理,如果每行的匹配数是常量的话

grep '!' encutkpoint_calculations/* | grep -oP '\d+(\.\d+)?' | pr -4ats' '

关于linux - grep --output-only 到单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49750701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com