Linux shell 编程之 grep 用正则表达式抓取匹配的内容

命令

grep [表达式] # 默认是将匹配的行全部输出
grep -o # 将仅与目标匹配的内容输出

实战

  • 文件ExpResult.txt,内容如下,我想把unmatchedKP=[0-100:179, 0-586:277, 0-367:267]字段中中括号间的部分[.....]取出来,然后顺便去掉前后括号,具体过程如下:
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.9312499999999999, KnowledgeCoverage=0.9, Difficulty=0.5375, Timeused=22.0, Count=50.0, timer=22.0, unmatchedKP=[0-297:294, 0-399:222]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=9.0, Count=24.0, timer=9.0, unmatchedKP=[0-297:294, 0-140:138, 0-367:267]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=12.0, Count=33.0, timer=12.0, unmatchedKP=[0-399:222, 0-65:179, 0-81:82]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=15.0, Count=42.0, timer=15.0, unmatchedKP=[0-100:179, 0-586:277, 0-367:267]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=16.0, Count=47.0, timer=16.0, unmatchedKP=[0-423:582, 0-367:267, 0-81:82]}
Experiment{meanscore=0.92625, meancountt=37.0, meantime=13.0}

# dif = 0.5
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=17.0, Count=67.0, timer=17.0, unmatchedKP=[0-410:72, 0-100:179, 0-240:277]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=17.0, Count=72.0, timer=17.0, unmatchedKP=[0-583:582, 0-586:277, 0-81:82]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=20.0, Count=92.0, timer=20.0, unmatchedKP=[0-423:582, 0-586:277, 0-367:267]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.925, KnowledgeCoverage=0.85, Difficulty=0.5, Timeused=7.0, Count=29.0, timer=7.0, unmatchedKP=[0-410:72, 0-100:179, 0-367:267]}
Experiment{Condition='RuleBean{difficulty=0.5, pointIdsNum=20}', ObjectFun=0.875, KnowledgeCoverage=0.75, Difficulty=0.5, Timeused=23.0, Count=100.0, timer=23.0, unmatchedKP=[0-410:72, 0-100:179, 0-240:277, 0-586:277, 0-81:82]}
Experiment{meanscore=0.915, meancountt=70.0, meantime=15.0}

# dif = 0.7
Experiment{Condition='RuleBean{difficulty=0.7, pointIdsNum=20}', ObjectFun=0.8952380952380953, KnowledgeCoverage=0.8, Difficulty=0.6904761904761905, Timeused=28.0, Count=100.0, timer=28.0, unmatchedKP=[0-410:72, 0-100:179, 0-240:277, 0-81:82]}
Experiment{Condition='RuleBean{difficulty=0.7, pointIdsNum=20}', ObjectFun=0.8977272727272727, KnowledgeCoverage=0.8, Difficulty=0.7045454545454546, Timeused=26.0, Count=100.0, timer=26.0, unmatchedKP=[0-100:179, 0-586:277, 0-140:138, 0-81:82]}
Experiment{Condition='RuleBean{difficulty=0.7, pointIdsNum=20}', ObjectFun=0.8812500000000001, KnowledgeCoverage=0.85, Difficulty=0.6125, Timeused=23.0, Count=100.0, timer=23.0, unmatchedKP=[0-100:179, 0-586:277, 0-81:82]}
Experiment{Condition='RuleBean{difficulty=0.7, pointIdsNum=20}', ObjectFun=0.9488636363636364, KnowledgeCoverage=0.95, Difficulty=0.6477272727272727, Timeused=21.0, Count=100.0, timer=21.0, unmatchedKP=[0-586:277]}
Experiment{Condition='RuleBean{difficulty=0.7, pointIdsNum=20}', ObjectFun=0.9154761904761904, KnowledgeCoverage=0.9, Difficulty=0.6309523809523809, Timeused=25.0, Count=100.0, timer=25.0, unmatchedKP=[0-410:72, 0-140:138]}
Experiment{meanscore=0.9077110389610389, meancountt=100.0, meantime=23.0}

cat ExpResult.txt  | grep  -o "\\[.*\\]" | sed "s/\[//g" | sed "s/\]//g"
  • 输出内容

在这里插入图片描述
下一篇将介绍,如何统计这些字段的频率link

猜你喜欢

转载自blog.csdn.net/Xurui_Luo/article/details/106797355