5

I have a link and I would like to return only content between www. and .com

e.g www.blablabla.com would return only blablabla

How could I do that? When I use grep '\.[a-zA-Z0-9\.-]*\.' it gives me .blablabla.

closed as unclear what you're asking by John1024, Jeff Schaller, Jakuje, garethTheRed, mdpc May 5 '16 at 20:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    awk -F. '{print $2}' – jasonwryan May 2 '16 at 18:07
  • Yes it would work but i forgot to mention that i do not want to use cut or awk to cut it with delimiter . – pnom May 2 '16 at 18:11
  • 3
    "i do not want to use cut or awk to cut it with delimiter ." Apparently, you don't want grep -P either. To get the best answer, you should explain your requirements. There are many good solutions to this problem. You should explain why you are rejecting the best ones. – John1024 May 2 '16 at 18:44
  • 2
    Homework problem? – mdpc May 5 '16 at 20:36
7
$ echo "www.blablabla.com" | grep -oP '(?<=\.)[a-zA-Z0-9\.-]*(?=\.)' 
blablabla

-o -- print only matched parts of matching line

-P -- Use Perl regex

(?<=\.) -- after a literal ., aka, a "positive look-behind" ...

[a-zA-Z0-9\.-]* -- match zero or more instances of lower & upper case characters, numbers 0-9, literal . and hyphen ...

(?=\.) -- followed by a literal ., aka a "positive look-ahead"

See this link for more on look arounds. Tools like https://regex101.com/ can help you break down your regular expressions.

  • Thanks that's what i wanted but what does it do ? Could u explain it a bit more please? Also -P uses Perl regular expression is there any way to do it just with grep regular expressions? – pnom May 2 '16 at 18:13
  • Not that I know of – KM. May 2 '16 at 18:39
  • 1
    If you don't want to use -P there is no way you can do this using grep re alone. If you want to stick with grep consider using tr to drop the . like this echo 'www.blablabla.com' | grep -o '\.[a-zA-Z0-9\.-]*\.' | tr -d . – GMaster May 3 '16 at 1:34
1

sed solution:

$ str='Hellowww.hello.comMywww.world.comWorld'

$ echo "$str" | sed -e 's/com/com\n/g' | sed -ne '/.*www\.\(.*\)\.com.*/{ s//\1/p }'
hello
world

Not the answer you're looking for? Browse other questions tagged or ask your own question.