1

I have the following code:

url='https://github.com/Project/name-app.git'
echo $url

I have to get this back to me, i.e. the name of the project regardless of the owner of the project.

Result:

name-app

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Paul
  • 3,644
  • 9
  • 47
  • 113
  • This is a poor duplicate. While it can be used to answer the question, it does not entirely do so. Voting to reopen. – Ryan M Sep 30 '21 at 08:28

4 Answers4

4

You can use string manipulation in Bash:

url='https://github.com/Project/name-app.git'
url="${url##*/}" # Remove all up to and including last /
url="${url%.*}"  # Remove up to the . (including) from the right
echo "$url"
# => name-app

See the online Bash demo.

Another approach with awk:

url='https://github.com/Project/name-app.git'
url=$(awk -F/ '{sub(/\..*/,"",$NF); print $NF}' <<< "$url")
echo "$url"

See this online demo. Here, the field delimiter is set to /, the last field value is taken and all after first . is removed from it (together with the .), and that result is returned.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I can ask you something that is always about this, so as not to have to reopen a new problem. – Paul Sep 30 '21 at 10:01
  • Yes. Ask here to keep everything related to the current problem in one thread. – Wiktor Stribiżew Sep 30 '21 at 10:09
  • I have the following code, I would like to get the result printed below. https://pastebin.com/syP0xzug The problem is if the mold without assigning it works correctly, if I assign it to a variable it does not work well. Also the part where it says code and endCode, I wish it could be solved with a single instruction with assignment to a variable, do you think it is feasible? – Paul Sep 30 '21 at 12:07
  • @Paul Is the `git@github.com-nameUser expo/react-native-read-more-text.git` output the expected output? – Wiktor Stribiżew Sep 30 '21 at 12:21
  • Result: `git@github.com-nameUser:expo/react-native-read-more-text.git` Find the comment at the end with the expected result. – Paul Sep 30 '21 at 12:34
  • 1
    @Paul See [this Bash demo](https://ideone.com/XT95O0). Initialize the two variables with `IFS=: read -r email url <<< "$url"` and then all you need is to combine the result from the three variables, `p="$email-$user:$url"`. – Wiktor Stribiżew Sep 30 '21 at 12:45
  • @Paul A `sed` solution is also possible, see [this demo](https://ideone.com/vpLLYj). – Wiktor Stribiżew Sep 30 '21 at 16:37
  • Thx, but last example the one with sed, doesn't seem to work. I did a test on: https://www.tutorialspoint.com/execute_bash_online.php – Paul Oct 01 '21 at 16:53
  • @Paul It is really weird, the first time I see this. For some unknown reason, `&` is not recognized as the backreference to the whole match in `sed` replacement (RHS, right-hand side). So, work around it using a capturing group, `p=$(echo "$url" | sed "s|\([^:]*\)|\1-$user|")` – Wiktor Stribiżew Oct 02 '21 at 10:15
  • Thx, it seems to work, I don't understand why the one before didn't work. – Paul Oct 03 '21 at 11:50
2

You can use grep regexp:

url='https://github.com/Project/name-app.git'
echo $url | grep -oP ".*/\K[^\.]*"

You can use bash expansion:

url='https://github.com/Project/name-app.git'
mytemp=${url##*/}
echo ${mytemp%.*}
Saboteur
  • 1,331
  • 5
  • 12
2

1st solution: With your shown samples please try following awk code.

echo $url | awk -F'\\.|/' '{print $(NF-1)}'

OR to make sure last value is always ending with .git try a bit tweak in above code:

echo "$url" | awk -F'\\.|/' '$NF=="git"{print $(NF-1)}'


2nd solution: Using sed try following:

echo "$url" | sed 's/.*\///;s/\..*//'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2
# my prefered solution
url='https://github.com/Project/name-app.git' 
basename $url .git
> name-app

# others
sed 's/\(.*\/\)\([^\/]*\)\.\(\w*\)$/\2/g'  <<<$url
1.https://github.com/Project/ 
2.name-app 
#
awk -F"[/.]" '{print $(NF-1)}' <<<$url
awk -F"[/.]" '{print $6}' <<<$url
tr '/.' '\n' <<<$url|tail -2|grep -v git
ufopilot
  • 3,269
  • 2
  • 10
  • 12