Bash:如何从字符串中删除多个前缀变量

Bash:如何从字符串中删除多个前缀变量,bash,sed,Bash,Sed,我正在尝试删除使用bash从package.json文件解析的url字符串的前缀。我发现的问题是,并非每个人都在其包文件中一致地进入repository:url 例如: git+https://github.com//.git https://github.com//.git 我还发现: http://github.com//.git 所以我的问题是,如何删除可能在.json中找到的字符串前缀?下面是我失败的尝试 repo_url="`node -pe 'JSON.parse(process.a

我正在尝试删除使用bash从package.json文件解析的url字符串的前缀。我发现的问题是,并非每个人都在其包文件中一致地进入repository:url

例如:

git+https://github.com//.git

https://github.com//.git

我还发现:

http://github.com//.git

所以我的问题是,如何删除可能在.json中找到的字符串前缀?下面是我失败的尝试

repo_url="`node -pe 'JSON.parse(process.argv[1]).repository.url' "$(cat $pkg_json)"`"

repo=${repo_url#git+}
repo=${repo_url#https://github.com/}
repo=${repo%.git}
echo "${repo}"
更新:

我发现在前缀之前添加一个通配符将消除前面的所有内容,包括前缀。但是如何处理
http

e、 g:


您对
repo
的每个作业都使用原始
$repo\u url
作为源,因此不会保留从上一个作业中删除的内容。您应该使用
$repo
作为源,但第一个除外:

repo_url="`node -pe 'JSON.parse(process.argv[1]).repository.url' "$(cat $pkg_json)"`"

repo=${repo_url#git+}
repo=${repo#https://github.com/}
repo=${repo%.git}
echo "${repo}"

awk
备选方案:

echo $repo_url|awk  '{print $(NF-2)"/"$(NF-1)}' FS='[./]'

repo="`node -pe 'JSON.parse(process.argv[1]).repository.url' "$(cat $pkg_json)" | awk  '{print $(NF-2)"/"$(NF-1)}' FS='[./]'`"

结果证明效果最好的是
repo=${repo\uURL}*://github.com/}repo=${repo%.git}echo“${repo}”
Thnx,但我需要将所有内容都保留为本机,而不让脚本启动单独的进程。
echo $repo_url|awk  '{print $(NF-2)"/"$(NF-1)}' FS='[./]'

repo="`node -pe 'JSON.parse(process.argv[1]).repository.url' "$(cat $pkg_json)" | awk  '{print $(NF-2)"/"$(NF-1)}' FS='[./]'`"