将 zsh 函数参数传递给 grep -E
Passing zsh function parameter to grep -E
背景...
正在尝试查找最后一次触及特定文件的提交。
我可以在从 git-log
到 grep
的 CLI 管道上执行此操作,但我试图将其包装在 zsh 函数中,更多的是为了便于记忆。
这是我的函数,这是我想用它生成的输出。
# match lines from git log that start with commit or include the
# filename I'm interested in and then pipe back through grep to color the output
glpg() {
\git log --name-only | \grep -E ‘“$"|^commit\s\S' | \grep -B1 --color -E ‘$'
}
所需的用法和输出
dwight:assets (add-analytics*) $ glpg clickouts
commit 6662418b8e68e478b95e7254faa6406abdada30f
web/assets/app/viewmodels/clickouts.js
web/assets/app/views/clickouts.html
web/client/app/viewmodels/clickouts.js
web/client/app/views/clickouts.html
--
commit cee37549f613985210c9caf90a48e2cca28d4412
web/client/app/viewmodels/clickouts.js
web/client/app/views/clickouts.html
--
commit df9ea8cd90ff80b89a0c7e2b0657141b105d5e7e
web/client/app/viewmodels/clickouts.js
web/client/app/views/clickouts.html
三个问题。
- 您使用 Unicode 撇号和引号,‘ 和 “。将它们替换为 ASCII 引号和双引号。
- 您不能使用 \s 和 \S 来表示 space 或非 space 以及标准 (POSIX) grep。使用
' '
和 [^ ]
以方便携带。
- 所有参数的列表都用
"$@"
引用,包括双引号。
背景...
正在尝试查找最后一次触及特定文件的提交。
我可以在从 git-log
到 grep
的 CLI 管道上执行此操作,但我试图将其包装在 zsh 函数中,更多的是为了便于记忆。
这是我的函数,这是我想用它生成的输出。
# match lines from git log that start with commit or include the
# filename I'm interested in and then pipe back through grep to color the output
glpg() {
\git log --name-only | \grep -E ‘“$"|^commit\s\S' | \grep -B1 --color -E ‘$'
}
所需的用法和输出
dwight:assets (add-analytics*) $ glpg clickouts
commit 6662418b8e68e478b95e7254faa6406abdada30f
web/assets/app/viewmodels/clickouts.js
web/assets/app/views/clickouts.html
web/client/app/viewmodels/clickouts.js
web/client/app/views/clickouts.html
--
commit cee37549f613985210c9caf90a48e2cca28d4412
web/client/app/viewmodels/clickouts.js
web/client/app/views/clickouts.html
--
commit df9ea8cd90ff80b89a0c7e2b0657141b105d5e7e
web/client/app/viewmodels/clickouts.js
web/client/app/views/clickouts.html
三个问题。
- 您使用 Unicode 撇号和引号,‘ 和 “。将它们替换为 ASCII 引号和双引号。
- 您不能使用 \s 和 \S 来表示 space 或非 space 以及标准 (POSIX) grep。使用
' '
和[^ ]
以方便携带。 - 所有参数的列表都用
"$@"
引用,包括双引号。