使用grep在两个字符之间提取子文本

时间:2015-08-04 13:40:32

标签: awk sed grep

我的文本文件包含以下信息

    @Mp_chzt_1
    asdjhsadhasdhdbjashdjaudashdjashdasdhasdhasdh
    asdasdkasjdkaskdskadkasdkasdkjaskldasdklasdas
    ahsjdasdfdfsdhghrtuztiuiuzozuoiouiouiouiouiou
    asjkjieqjeroiweoriksfjksjksjkf
    +
    !!!@!!!!!!!!++??????????????~~~~~~~~~~~~~
    BBBBBBBBBBBBMMMMMM!!!!!++LLLLLL******
    @Mp_btrea_1
    uokjjkzghqawsdasduihdlöklöaklöskdlkaökgzgzggz
    asdasduzuqwtzeqweuvixcvdjfiisduiifuzwpqüqwoeü
    kjkjiuijwiqquzwuziziqz
    +
    **********||||||||||||##@######++++?????????
    MMMMMMMMMUUUU***+++~~~~~~~~~~~~~~~~~~~~~~~~~~
    @Mp_trwe_3
    jhtrqhkjiqkjkqwjelasjjljiewkjkljkldjflsjljki8u
    immhgwqtzopirpjgbsdkfjieipwippieoroeirkvsdjjfk
    jkahdjhjhfuhjkwekksjakjeiuwiurweiurioweuroweod
    poplrtm,ernmjhazqweqwjidiipfiopdifosidpfppsdif
    mnasnbdhgqweqweipoipoxkajksdökalsklsaksldkasöd
    asdas
    +
    !!!!!!!!!!!!!!!!!!@@@@@???????????????????

我想在文本正下方的@ Mp_ *和+之间提取区域并将其导出到txt文件,如下所示

    @Mp_chzt_1
    asdjhsadhasdhdbjashdjaudashdjashdasdhasdhasdh
    asdasdkasjdkaskdskadkasdkasdkjaskldasdklasdas
    ahsjdasdfdfsdhghrtuztiuiuzozuoiouiouiouiouiou
    asjkjieqjeroiweoriksfjksjksjkf
    @Mp_btrea_1
    uokjjkzghqawsdasduihdlöklöaklöskdlkaökgzgzggz
    asdasduzuqwtzeqweuvixcvdjfiisduiifuzwpqüqwoeü
    kjkjiuijwiqquzwuziziqz
    @Mp_trwe_3
    jhtrqhkjiqkjkqwjelasjjljiewkjkljkldjflsjljki8u
    immhgwqtzopirpjgbsdkfjieipwippieoroeirkvsdjjfk
    jkahdjhjhfuhjkwekksjakjeiuwiurweiurioweuroweod
    poplrtm,ernmjhazqweqwjidiipfiopdifosidpfppsdif
    mnasnbdhgqweqweipoipoxkajksdökalsklsaksldkasöd
    asdas

当我使用以下代码时

grep -o -P '(?<=@MP.*).*(?=+)' query.txt > output.txt

它给了我“grep:没有什么可重复的”。 任何人都可以指导我的错误在哪里以及如何纠正它。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

最好使用awk

awk '/^@/{f=1} /^+/ {f=0} f' file > output.txt

或者,如果您有前导空格,请将其与\s*匹配:

awk '/^\s*@/{f=1} /^\s*\+/ {f=0} f' file > output.txt

这使用标记f来决定是否应该打印该行。

  • 当它看到以@开头的行时,会激活它。
  • 当它看到以+开头的行时,会停用它。
  • 然后,它会评估该标志并打印它是否为True。

根据您的输入,它返回:

@Mp_chzt_1
asdjhsadhasdhdbjashdjaudashdjashdasdhasdhasdh
asdasdkasjdkaskdskadkasdkasdkjaskldasdklasdas
ahsjdasdfdfsdhghrtuztiuiuzozuoiouiouiouiouiou
asjkjieqjeroiweoriksfjksjksjkf
@Mp_btrea_1
uokjjkzghqawsdasduihdlöklöaklöskdlkaökgzgzggz
asdasduzuqwtzeqweuvixcvdjfiisduiifuzwpqüqwoeü
kjkjiuijwiqquzwuziziqz
@Mp_trwe_3
jhtrqhkjiqkjkqwjelasjjljiewkjkljkldjflsjljki8u
immhgwqtzopirpjgbsdkfjieipwippieoroeirkvsdjjfk
jkahdjhjhfuhjkwekksjakjeiuwiurweiurioweuroweod
poplrtm,ernmjhazqweqwjidiipfiopdifosidpfppsdif
mnasnbdhgqweqweipoipoxkajksdökalsklsaksldkasöd
asdas