grep --output-only到单行

时间:2018-04-10 09:57:06

标签: linux grep

我目前正在运行一个非常接近我想要的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

1 个答案:

答案 0 :(得分:1)

由于数据也是从文件名中提取的,因此我将首先使用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' '