最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

grep

运维笔记admin5浏览0评论

grep

grep

grep - 只输出到单行上(grep --output-only onto single line)

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

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

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

7.969802152.038867677.969804152.101124737.969806152.102003987.969808152.10203172

我宁愿得到表格的输出:

7.969 80 2 152.038867677.969 80 4 152.101124737.969 80 6 152.102003987.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 Ryencutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=4.out:! total energy = -152.10112473 Ryencutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=6.out:! total energy = -152.10200398 Ryencutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=8.out:! total energy = -152.10203172 Ry

I'm currently running a grep that is very close to what I want:

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

I like that this command only returns the matching contents of the line, but want the output to be on a line by line basis. E.g. when I run the command currently I get:

7.969802152.038867677.969804152.101124737.969806152.102003987.969808152.10203172

Where I would rather get output of the form:

7.969 80 2 152.038867677.969 80 4 152.101124737.969 80 6 152.102003987.969 80 8 152.10203172

I could write a script or use vim on the output, but I suspect there is a more elegant solution...

PS the source file looks like:

$grep ! encutkpoint_calculations/* | grep -P '\d+\.\d+|\d+'encutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=2.out:! total energy = -152.03886767 Ryencutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=4.out:! total energy = -152.10112473 Ryencutkpoint_calculations/MgO.scf.a=7.969.ecut=80.k=6.out:! total energy = -152.10200398 Ryencutkpoint_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.038867677.969 80 4 152.101124737.969 80 6 152.102003987.969 80 8 152.10203172

如果匹配次数每行不变,交替就是后处理数据

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

Since data is being extracted from file names as well, I'll leave the first use of grep as is

$ # 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.038867677.969 80 4 152.101124737.969 80 6 152.102003987.969 80 8 152.10203172

Alternate is to post process the data, if number of matches is constant per line

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

发布评论

评论列表(0)

  1. 暂无评论