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 RyI'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.10203172Where 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.10203172I 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.10203172Alternate is to post process the data, if number of matches is constant per line
grep '!' encutkpoint_calculations/* | grep -oP '\d+(\.\d+)?' | pr -4ats' '