Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the string between two dots in bash?

I have several git-Repos in such format:

product.module1.git
product.module2.git
...

Now I just want to iterate over the list to get just

module1
module2

How can I achieve this? I've already tried ls in combination with grep, but I'm not able to remove the first and last string parts.

like image 930
Martin Avatar asked Feb 09 '23 06:02

Martin


1 Answers

If your grep supports the -P option:

$ grep -oP '(?<=[.])\w+(?=[.])' file
module1
module2

(?<=[.]) is a look behind. It matches, in this case, just after a period.

\w+ matches any number of word characters.

(?=[.]) is a look ahead. It matches, in this case, just before a period.

like image 160
John1024 Avatar answered Feb 13 '23 04:02

John1024