2012-07-29 63 views
1

如何在不輸入排除模式的情況下排除ack排除的文件(臨時文件和版本控制目錄)?如何將grep配置爲更像ack?

作爲described here,你可以使用GREP_OPTIONS,但是然後你運行到problems here:使用grep而不清除GREP_OPTIONS的腳本將會中斷。

什麼是更好的方法?別名?或者其他包裝grep的腳本?

雖然明顯的答案是alias grep="ack -a",但我對如何在不使用ack的情況下解決此問題感興趣。

+0

另外:ack 2.0可以消除'-a'和'-u'標誌。 2.0中的默認值是選擇所有文本文件進行搜索,而不管文件類型如何。 – 2012-07-30 14:01:36

+0

另外,請注意'ack -a'仍然不等於'grep',因爲ack仍然會忽略某些目錄和文件。 '-a'表示「所有文件類型」,而不是「所有文件」。 – 2013-04-11 21:34:47

+0

從grep 2.20開始,不推薦使用GREP_OPTIONS:https://lists.gnu.org/archive/html/grep-commit/2014-09/msg00007.html – idbrii 2015-08-05 16:22:35

回答

0

我目前的解決方案是使用別名來限制我的智能選項對我的交互式輸入(而不是其他人的腳本)的使用。這是從我的bash_aliases grep的相關片段:

SMART_GREP_OPTIONS= 

# Ensure colors are supported. 
if [ -x /usr/bin/dircolors ]; then 
    SMART_GREP_OPTIONS=' --color=auto' 
fi 


# Make grep more like ack. 
# Ignore binary files. 
SMART_GREP_OPTIONS="$SMART_GREP_OPTIONS --binary-files=without-match" 
# Check for exclude-dir to prevent invalid options in older versions of grep. 
# Assume if we have exclude-dir, that we have exclude. 
if /bin/grep --help | /bin/grep -- --exclude-dir &>/dev/null; then 
    # Ignore version control folders. 
    for PATTERN in .cvs .git .hg .svn; do 
     SMART_GREP_OPTIONS="$SMART_GREP_OPTIONS --exclude-dir=$PATTERN" 
    done 
    # Ignore temp files. 
    for PATTERN in *.swp; do 
     SMART_GREP_OPTIONS="$SMART_GREP_OPTIONS --exclude=$PATTERN" 
    done 
fi 
# This alias may override ghostscript. That's not a problem for me. 
alias gs='grep $SMART_GREP_OPTIONS' 

我做了我的大部分grepping從vim和使用vim-searchsavvy設置類似的選項(我不使用我的別名從VIM)。