从 grep 中删除行号选项
Remove line number option from grep
我使用 grep 的别名,将行号添加到输出中:
alias grep="grep -n -I --color"
对于某个命令,我想从 grep 命令中删除 -n
。我找不到 "Do not add line numbers" 标志。有这样的东西吗?还是我必须添加一个没有行号的额外别名?
您可以使用 command
删除 所有 个参数。
command grep -I --color
只需使用反斜杠,因为它将恢复为默认的 grep
命令,而不是您的别名:
\grep -I --color
我意识到这是一个较旧的问题,但我 运行 遇到过同样的问题。此解决方案有效,但不是最优雅的:
# Not part of your original question, but I learned that if an alias
# contains a trailing space, it will use alias expansion. For the case
# of 'xargs', it allows you to use the below grep aliases, such as the
# 'chgrep' alias defined below:
# grep -l 'something' file1 file2 ... | xargs chgrep 'something else'
alias xargs='xargs '
export GREP_OPT='-n'
# Note the $(...) INSIDE the 'single quotes'
alias grep_='\grep --color=auto -I $(echo "$GREP_OPT")'
# Due to alias expansion, the below aliases benefit from the 'GREP_OPT' value:
alias cgrep='grep_ -r --include=\*.{c,cc,cpp}'
alias hgrep='grep_ -r --include=\*.{h,hh,hpp}'
alias chgrep=cgrep_ --include=\*.{h,hh,hpp}' # Not sure if there's a way to combine cgrep+hgrep
...
alias pygrep='grep_ -r --include=\*.py'
在您想要 non-numbered 输出的情况下,取消设置 GREP_OPT
(或从中删除 -n
,就像您使用它来包含其他标志一样)。
我在上面的例子中使用了 grep_
而不是 grep
只是为了表明别名会很高兴地 chain/use 根据需要一直扩展到原始命令的别名。请随意将它们命名为 grep
。您会注意到原始 grep_
使用命令 \grep
因此别名扩展在该点停止。
如果有更好的方法,我完全愿意接受建议(因为 setting/unsetting GREP_OPT
并不总是最优雅的)。
我使用 grep 的别名,将行号添加到输出中:
alias grep="grep -n -I --color"
对于某个命令,我想从 grep 命令中删除 -n
。我找不到 "Do not add line numbers" 标志。有这样的东西吗?还是我必须添加一个没有行号的额外别名?
您可以使用 command
删除 所有 个参数。
command grep -I --color
只需使用反斜杠,因为它将恢复为默认的 grep
命令,而不是您的别名:
\grep -I --color
我意识到这是一个较旧的问题,但我 运行 遇到过同样的问题。此解决方案有效,但不是最优雅的:
# Not part of your original question, but I learned that if an alias
# contains a trailing space, it will use alias expansion. For the case
# of 'xargs', it allows you to use the below grep aliases, such as the
# 'chgrep' alias defined below:
# grep -l 'something' file1 file2 ... | xargs chgrep 'something else'
alias xargs='xargs '
export GREP_OPT='-n'
# Note the $(...) INSIDE the 'single quotes'
alias grep_='\grep --color=auto -I $(echo "$GREP_OPT")'
# Due to alias expansion, the below aliases benefit from the 'GREP_OPT' value:
alias cgrep='grep_ -r --include=\*.{c,cc,cpp}'
alias hgrep='grep_ -r --include=\*.{h,hh,hpp}'
alias chgrep=cgrep_ --include=\*.{h,hh,hpp}' # Not sure if there's a way to combine cgrep+hgrep
...
alias pygrep='grep_ -r --include=\*.py'
在您想要 non-numbered 输出的情况下,取消设置 GREP_OPT
(或从中删除 -n
,就像您使用它来包含其他标志一样)。
我在上面的例子中使用了 grep_
而不是 grep
只是为了表明别名会很高兴地 chain/use 根据需要一直扩展到原始命令的别名。请随意将它们命名为 grep
。您会注意到原始 grep_
使用命令 \grep
因此别名扩展在该点停止。
如果有更好的方法,我完全愿意接受建议(因为 setting/unsetting GREP_OPT
并不总是最优雅的)。