简体   繁体   English

Grep文字仅在[word]之后:

[英]Grep text only only after [word]:

I have a file example.txt which contains following text: 我有一个example.txt文件,其中包含以下文本:

[one]: bla bla bla onebla twobla
[two]: hey heya noheya
[onemore]: i got mad and etc

I need to grep and show only text that after [myword]: 我需要grep并仅显示在[myword]:之后的文本[myword]:

Tried to test grep [myword] /tmp/example.txt | cut -d ':' -f 2 试图测试grep [myword] /tmp/example.txt | cut -d ':' -f 2 grep [myword] /tmp/example.txt | cut -d ':' -f 2

On each [myword] it prints all after brackets, but how can I get only the one I need only and not all? 在每个[myword]它都打印在方括号后,但是我如何只得到只需要而不是全部的呢?

You can cut the first phrase [something]: and get the rest using 您可以剪切第一个词组[something]:然后使用

grep and sed: grep和sed:

$ grep -w "\[onemore\]:" words.txt | sed 's/\[onemore\]://g'
 i got mad

grep and awk: grep和awk:

$ grep -w "\[onemore\]:" words.txt | awk -F: '{print $2}'
 i got mad

Thanks to all! 谢谢大家! Got an answer on other from Unix & Linux: 在Unix和Linux上得到了另一个答案:

$ awk '/\\[myword\\]/{sub(/^[^]]*\\]:[[:blank:]]*/,"");print}' example.txt

OR 要么

$ awk '/\\[myword\\]/{sub(/^[^]]*\\]:[[:blank:]]*/,"");print}' example.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM