0

I have a file example.txt which contains following text:

[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]:

Tried to test 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?

Benjamin W.
  • 33,075
  • 16
  • 78
  • 86
alya
  • 27
  • 1
  • 6
  • There is no `[myword]` in input – anubhava Aug 17 '16 at 16:48
  • what are you trying to do? – justaguy Aug 17 '16 at 18:27
  • I need to print only the test that after certain brackets. For example I want to print only text that in line `[one]:` but not after `[two]:` or `[onemore]:`lines, so that the output will be `bla bla bla onebla twobla`. If I want to print all after `[onemore]:` - the correct output should be `i got mad and etc` – alya Aug 18 '16 at 07:02

2 Answers2

0

You can cut the first phrase [something]: and get the rest using

grep and sed:

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

grep and awk:

$ grep -w "\[onemore\]:" words.txt | awk -F: '{print $2}'
 i got mad
Kristo Mägi
  • 1,271
  • 8
  • 13
  • I need to get output of this for example: 'Grep [one more]: words.txt' Result that I need to get: 'I got mad' – alya Aug 17 '16 at 20:28
  • @alya whatever is your word/phrase add it into double quotes: `grep -o -w "i got mad" words.txt` Will result to: `i got mad` – Kristo Mägi Aug 17 '16 at 20:34
  • @alya: I'm sorry for the last comment, wasn't looking exact grep input you posted. I edited my answer to give you what you wanted. – Kristo Mägi Aug 17 '16 at 20:51
  • Thank you @Kristo, but it doesn't work as needed. Please see my comment above. – alya Aug 18 '16 at 07:07
  • @alya: I did read your comment and edited my post accordingly. If the edited solution doesn't work as needed please elaborate more how one of those the solution doesn't work exactly. – Kristo Mägi Aug 18 '16 at 08:51
0

Thanks to all! Got an answer on other from Unix & Linux:

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

OR

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

alya
  • 27
  • 1
  • 6