Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep string after what I specify [duplicate]

Tags:

linux

grep

I am using grep and I want to get the data after my string I specify. I want to get the string after what I specify. If I grep "label:" I get "label: blue label:red label: green". I want to get just the colors. If I say grep -o I get all the labels. If I say -l it says (standard output). Any idea?

like image 372
mike Avatar asked Sep 17 '11 01:09

mike


2 Answers

you can use sed to eliminate the word label e.g.

grep "label:" | sed s/label://g
like image 75
Eric Fortis Avatar answered Oct 17 '22 11:10

Eric Fortis


Grep is a tool that can only be used for filtering lines of text. To do anything more complex, you probably need a more complex tool.

For example, sed can be used here:

sed 's/.*label: \(.*\)/\1/'

Text like this...

label: blue
llabel: red
label: green label: yellow

Gets turned into this:

blue
red
yellow

As you can see, this sed command only returns what comes after your search pattern, and multiple matches per lines aren't supported.

like image 2
Dan Cecile Avatar answered Oct 17 '22 09:10

Dan Cecile