使用bash模式之间grep的文字 [英] Grep text between patterns using bash

查看:89
本文介绍了使用bash模式之间grep的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要驻留在txt文件中的数据进行排序。样本数据如下:

  ==
为Jhon
母鹿
得分了 -
------
======

工匠
得分+
------
======

骨髓
得分了 -
------

和我需要提取仅在分数+ 定义部分。这样的结果应

  ==

工匠
得分+
------


解决方案

桑达可以如下做到这一点:

  SED -n'/ ^ ====== / {:一; N; / \\ n ------ / BA /分+ / p!} INFILE
======

工匠
得分+
------

其中, -n prevents印刷,以及

  / ^ == / {#如果模式空间开头==
    :一个#标签跳转至
    N#追加下一行到模式空间
    !/ \\ n ------ / BA#如果我们不匹配------分支:一个
    /分+ / p#如果我们匹配分数+,打印模式空间
}

事情可能与 / \\ n $ ------ / 更正确固定,但也有在线路末端的空间,我不知道如果这些都是真实的或复制粘贴文物&ndash的;但这项工作的示例数据。

I need to sort data which reside in txt file. The sample data is as follows:

======
Jhon 
Doe 
score -
------  
======
Ann 
Smith 
score + 
------
======
Will 
Marrow 
score - 
------

And I need to extract only sections where score + is defined. So the result should be

======
Ann 
Smith 
score + 
------

解决方案

Sed can do it as follows:

sed -n '/^======/{:a;N;/\n------/!ba;/score +/p}' infile 
======
Ann 
Smith 
score + 
------

where -n prevents printing, and

/^======/ {       # If the pattern space starts with "======"
    :a            # Label to branch to
    N             # Append next line to pattern space
    /\n------/!ba # If we don't match "------", branch to :a
    /score +/p    # If we match "score +", print the pattern space
}

Things could be more properly anchored with /\n------$/, but there are spaces at the end of the lines, and I'm not sure if those are real or copy-paste artefacts – but this work for the example data.

这篇关于使用bash模式之间grep的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆