为什么这个来自 git ls-remote 的正则表达式不能正常工作?
why this regex from git ls-remote is not working as it should be?
如果我这样做:
echo From ssh://azeaze@azaz.git | grep -oh From.* | grep -oh ssh.*git
我得到了例外情况,即:
ssh://azeaze@azaz.git
但如果我这样做:
git ls-remote | grep -oh From.* | grep -oh ssh.*git
我得到以下输出:
From 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.
通配符扩展而引用的模式
如果我这样做:
echo From ssh://azeaze@azaz.git | grep -oh From.* | grep -oh ssh.*git
我得到了例外情况,即:
ssh://azeaze@azaz.git
但如果我这样做:
git ls-remote | grep -oh From.* | grep -oh ssh.*git
我得到以下输出:
From 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.