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?
Asked
Active
Viewed 6,181 times
2 Answers
6
you can use sed
to eliminate the word label e.g.
grep "label:" | sed s/label://g

Eric Fortis
- 16,372
- 6
- 41
- 62
-
... though the `grep` is [useless](http://www.iki.fi/era/unix/award.html#grep); `sed -n '/label:/s///gp'` – tripleee Feb 18 '16 at 08:24
2
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.

Dan Cecile
- 2,403
- 19
- 21