【问题标题】:extract lines between same keyword that match a pattern提取与模式匹配的相同关键字之间的行
【发布时间】:2020-04-22 17:58:29
【问题描述】:

需要匹配文件中唯一的模式,但需要在匹配模式的两个标记之间打印行。

我的文件看起来像这样。

echo "Start 2A25.20090401.64809.7.HDF 6420 6751"
echo "dimensions 9249 49"
echo "New Cell"
grep "6542,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 20.09 8.07334 74.6131 170 0 6 6
grep "6542,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 32.25 8.11139 74.6406  210 3.66764
grep "6543,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 33.28 8.05147 74.6431  210 0.84248
grep "6543,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 43.38 8.08952 74.6707  210 20.3994
grep "6543,08" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 24.22 8.12717 74.6979  210 1.21783
grep "6544,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 35.81 8.02963 74.6732  210 6.31353
grep "6544,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 41.58 8.06767 74.7007  200 14.5371
grep "6545,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 36.3 8.00776 74.7033  120 6.13395
grep "6545,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 31.57 8.0458 74.7308  210 4.22794
grep "6546,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 28.49 7.98589 74.7333  292 2.64533
echo "New Cell"
grep "6562,21" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 26.74 8.19021 75.6125 210 0.61061 9 9
grep "6563,20" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 26.35 8.13187 75.6167  210 1.0852
grep "6563,21" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 42.51 8.16825 75.6426  200 13.5489
grep "6563,22" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 25.82 8.20457 75.6684  210 0.615512
grep "6564,20" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 23.08 8.10994 75.6467  272 0.613962
grep "6564,21" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 46.55 8.14632 75.6726  200 17.1675
grep "6564,22" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 36.89 8.18263 75.6984  200 3.10095
grep "6565,21" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 31.61 8.12436 75.7026  200 2.52639
grep "6565,22" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 28.85 8.16067 75.7284  120 0.945648
echo "New Cell"

我需要 sed 来匹配模式并打印单元格中模式匹配的所有行。 例如对于“6545,06”作为模式,我需要模式匹配的“新单元格”边界之间的所有行,因为这个模式需要输出为

grep "6542,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 20.09 8.07334 74.6131 170 0 6 6
grep "6542,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 32.25 8.11139 74.6406  210 3.66764
grep "6543,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 33.28 8.05147 74.6431  210 0.84248
grep "6543,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 43.38 8.08952 74.6707  210 20.3994
grep "6543,08" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 24.22 8.12717 74.6979  210 1.21783
grep "6544,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 35.81 8.02963 74.6732  210 6.31353
grep "6544,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 41.58 8.06767 74.7007  200 14.5371
grep "6545,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 36.3 8.00776 74.7033  120 6.13395
grep "6545,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 31.57 8.0458 74.7308  210 4.22794
grep "6546,06" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 28.49 7.98589 74.7333  292 2.64533

不幸的是,开始和结束的界限是一样的。 如果我能获得一个 sed 脚本来执行此操作,将不胜感激。

【问题讨论】:

  • 你用了什么正则表达式?
  • 我尝试了几个关键字不同但在我的情况下关键字(新单元格)是相同的示例。我尝试了 sed 示例,但在我的情况下不起作用
  • 本网站的规则是您首先向我们展示您尝试过的内容。基于此,我们可以介入并为您澄清问题。因此,请向我们展示您尝试过但没有奏效的方法。
  • sed -e '1,/6544,06/d' -e '/New Cell/,$d' CSR.WoSpix.apr2009.sh grep "6544,07" ../TextFilesDir/ out.2a25.20090401.64809.7.hdf.txt.text = 41.58 8.06767 74.7007 200 14.5371 grep“ 6545,06” ../ textfilesdir/textfilesdir/.2A2A2A2A2A2A2.2A2.2A "6545,07" ../TextFilesDir/out.2A25.20090401.64809.7.HDF.txt.text = 31.57 8.0458 74.7308 210 4.22794 grep "6546,06" ../TextFilesDir/out.2A25.20090401.64809. txt.text = 28.49 7.98589 74.7333 292 2.64533
  • 这是打印图案下方的线条 (6544,06)。不在上面,我希望在未发生的输出中保留新单元格

标签: sed


【解决方案1】:

我现在没有sed,但下面的正则表达式正是你想要的(如果我理解正确的话):

echo "New Cell"\s*(.*?"6542,06".*?)\s*echo "New Cell"

您只能使用\1 提取“grep”行。

替换正则表达式中的"6542,06" 部分以找到其他子字符串。

我测试了正则表达式here

【讨论】:

  • 谢谢。我会尝试在 sed 中使用这个正则表达式并更新你。
【解决方案2】:

这可能对你有用(GNU sed):

sed '/New Cell/ba;H;$!d;:a;x;/6546,06/s/.//p;z;x;d' file

在保持空间 (HS) 中收集包含 New Cell 的行之后的行。

如果遇到包含New Cell 的另一行或到达文件末尾,请检查集合中所需的字符串(上例中的6546,06),并打印集合减去第一个字符,该字符将是引入了换行符。

无论是否匹配,清空 HS 并重复。

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多