Regex 如何在赛前和赛后对图案进行sed
我目前正在尝试从url获取特定参数 我的url看起来像:Regex 如何在赛前和赛后对图案进行sed,regex,bash,sed,Regex,Bash,Sed,我目前正在尝试从url获取特定参数 我的url看起来像:https://private.io/report-artifact/dsop-pipeline-artifacts/container-scan-reports/redhat/ubi/ubi7/7.8/2020-02-14T222203.548_2868/ubi7-7.8.tar 我只想要redhat/ubi/ubi7/7.8 我可以通过以下操作获得redhat/ubi/ubi7/7.8/2020-02-14T222203.548_286
https://private.io/report-artifact/dsop-pipeline-artifacts/container-scan-reports/redhat/ubi/ubi7/7.8/2020-02-14T222203.548_2868/ubi7-7.8.tar
我只想要redhat/ubi/ubi7/7.8
我可以通过以下操作获得redhat/ubi/ubi7/7.8/2020-02-14T222203.548_2868/ubi7-7.8.tar
echohttps://private.io/report-artifact/dsop-pipeline-artifacts/container-scan-reports/redhat/ubi/ubi7/7.8/2020-02-14T222203.548_2868/ubi7-7.8.tar |sed的|.*/container scan reports/| |'
因此,我想删除/2020-02-14T222203.548_2868/ubi7-7.8.tar
我还想将
/
更改为-
,这样我就有了带有GNUsed的redhat-ubi-ubi7-7.8
:
在*/container scan reports/
之后获取以下4个路径元素,并将所有/
替换为-
:
url='https://private.io/report-artifact/dsop-pipeline-artifacts/container-scan-reports/redhat/ubi/ubi7/7.8/2020-02-14T222203.548_2868/ubi7-7.8.tar'
echo "$url" | sed -E 's|.*/container-scan-reports/(([^/]*/){3}[^/]*).*|\1|;s|/|-|g'
或者您可以在*/container scan reports/
之后获取所有内容,但不能获取最后两个路径元素:
echo "$url" | sed -E 's|.*/container-scan-reports/(.*)/[^/]*/[^/]*|\1|;s|/|-|g'
使用GNUsed
:
在*/container scan reports/
之后获取以下4个路径元素,并将所有/
替换为-
:
url='https://private.io/report-artifact/dsop-pipeline-artifacts/container-scan-reports/redhat/ubi/ubi7/7.8/2020-02-14T222203.548_2868/ubi7-7.8.tar'
echo "$url" | sed -E 's|.*/container-scan-reports/(([^/]*/){3}[^/]*).*|\1|;s|/|-|g'
或者您可以在*/container scan reports/
之后获取所有内容,但不能获取最后两个路径元素:
echo "$url" | sed -E 's|.*/container-scan-reports/(.*)/[^/]*/[^/]*|\1|;s|/|-|g'
知道字符串中的位置后,可以使用cut
echo "${string}" | cut -d/ -f 7-10 | tr '/' '-'
sed的另一种方法是
echo "${string}" | sed -E 's#([^/]*/){6}([^/]*)/([^/]*)/([^/]*)/([^/]*).*#\2-\3-\4-\5#'
知道字符串中的位置后,可以使用cut
echo "${string}" | cut -d/ -f 7-10 | tr '/' '-'
sed的另一种方法是
echo "${string}" | sed -E 's#([^/]*/){6}([^/]*)/([^/]*)/([^/]*)/([^/]*).*#\2-\3-\4-\5#'
sed-E的| ^([^/]*/){6}| |;s |(/[^/]*){2}$| |;s |/|-| g'
sed-E的| ^([^/]*/){6}| |;s |(/[^/]*){2}$| |;s |/|-| g'