这个问题的最佳答案表明,cut可以使用 withtr来根据重复的空格进行剪切
< file tr -s ' ' | cut -d ' ' -f 8
Run Code Online (Sandbox Code Playgroud)
我想要获取目录中多个 Git 存储库的远程地址,并尝试使用以下内容从每个存储库中提取远程 URL 字段:
ls | xargs -I{} git -C {} remote -vv | sed -n 'p;n' | tr -s " " | cut -d ' ' -f1
Run Code Online (Sandbox Code Playgroud)
但是,这会导致(例如)以下输出,其中我可以看到保留了两个连续的空格(Unicode 代码点 32):
origin https://github.com/jik876/hifi-gan.git
origin https://github.com/NVIDIA/NeMo.git
origin https://github.com/NVIDIA/tacotron2.git
Run Code Online (Sandbox Code Playgroud)
(我也使用xargs过tr)
所需的输出是 URL 列表,例如:
https://github.com/jik876/hifi-gan.git
https://github.com/NVIDIA/NeMo.git
https://github.com/NVIDIA/tacotron2.git
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
那是一个制表符,而不是两个空格。
您可以使用 shell 循环迭代当前工作目录中具有目录的子目录.git,然后迭代cut第一个空格分隔的字段(以删除末尾添加的(fetch)和标签),然后将其传递到每个远程+URL 只显示一行:(push)gituniq
for r in ./*/.git/; do
git -C "$r" remote -v
done | cut -f 1 -d ' ' | uniq | cut -f 2
Run Code Online (Sandbox Code Playgroud)
决赛cut -f 2通过返回第二个制表符分隔字段来隔离 URL。
考虑到awk制表符和空格的处理方式相同(除非您使用特定的分隔符或模式),我们可以通过一次调用来替换尾随管道awk:
for r in ./*/.git/; do
git -C "$r" remote -v
done | awk '!seen[$2]++ { print $2 }'
Run Code Online (Sandbox Code Playgroud)