如果我这样做:
回显来自 ssh://azeaze@azaz.git | grep -oh 来自.* | grep -oh ssh.*git
我得到了异常(exception)情况,即:
ssh://azazaze@azaz.git
但如果我这样做:
git ls-remote | grep -oh 来自.* | grep -oh ssh.*git
我得到以下输出:
来自 ssh://azeaze@azaz.git
为什么?
最佳答案
“from”行转到 stderr,因此它会被 grep 忽略。将 stderr 重定向到 stdout 以将其通过管道传输到 grep:
git ls-remote 2>&1 | grep -o 'From.*' | grep -o 'ssh.*git'
小改动:grepping stdin 时不需要 -h
。引用模式以防止 shell 进行通配符扩展。
关于regex - 为什么这个来自 git ls-remote 的正则表达式不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59647504/