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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this community$ 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.
-P
uses Perl regular expression is there any way to do it just with grep regular expressions?
-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 .
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
awk -F. '{print $2}'
.
.
" Apparently, you don't wantgrep -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.