首页 > 解决方案 > 匹配单词组并打印然后仅使用 sed 或 awk

问题描述

使用 sed 或 awk ,我如何仅匹配和organization打印repo

git@github.org.com/organization/repo.git?

我的意图是将这两个值分配给变量,以便在我正在构建的东西中使用,以便为组织和存储库创建本地文件夹。

期望的结果将是organization/repo唯一的。

目前为了继续我正在使用cut -f2 -d':' | cut -f1 -d',但我对 push this 感到羞耻。

标签: bashawksed

解决方案


Since you need to create 2 shell variables it may be better to prefer a BASH solution.

You can use read with IFS='./'::

IFS='./:' read -ra arr <<< 'git@github.org.com/organization/repo.git'
echo "${arr[3]}/${arr[4]}"

organization/repo
  • IFS='./:' splits input string on DOT and / and :colon
  • read -a populates an array from input text

推荐阅读