Grep or sed for a word containing a string

file example:

blahblah 123.a.site.com some-junk yoyoyoyo 456.a.site.com more-junk hihohiho 123.a.site.org junk-in-the-trunk lalalala 456.a.site.org monkey-junk 

I want grep to have all these domains in the middle of each line, they all have a common a.site part that I can grep with, but I can’t decide how to do this without returning the entire line

Maybe sed or regex is needed here, since just grep is not enough?

+4
source share
2 answers

You can do:

 grep -o '[^ ]*a\.site[^ ]*' input 

or

 awk '{print $2}' input 

or

 sed -e 's/.*\([^ ]*a\.site[^ ]*\).*/\1/g' input 
+8
source

Try to find something in this position.

  $ sed -r "s/.* ([0-9]*)\.(.*)\.(.*)/\2/g" [0-9]* - For match number zero or more time. .* - Match anything zero or more time. \. - Match the exact dot. () - Which contain the value particular expression in parenthesis, it can be printed using \1,\2..\9. It contain only 1 to 9 buffer space. \0 means it contain all the expressed pattern in the expression. 
+3
source

Source: https://habr.com/ru/post/1380378/


All Articles