2008-10-16 84 views
3

我需要在svn工作副本中搜索「foo」中的所有cpp/h文件,完全不包括svn的特殊文件夾。什麼是精確命令爲GNU grep?svn工作副本快速遞歸grey

+0

正如Frentos所說,如果你花時間去查閱手冊頁,你的問題本來會很容易回答。 – freespace 2008-10-16 10:55:06

+2

嘿freespace,爲什麼不回答「簡單」的問題?如果它是正確的,我會很樂意接受你的回答。 – Constantin 2008-10-21 16:19:19

回答

9

爲此我使用ack,它就像grep,但自動知道如何排除源控制目錄(以及其他有用的東西)。

+2

請注意,Ubuntu/Debian上的ack包實際上被稱爲「ack-grep」,因爲有一個名爲「ack」的漢字代碼轉換器。 – JesperE 2008-10-16 11:08:07

1

這是一個RTFM。我輸入'man grep'和'/ exclude'得到:

--exclude = GLOB 跳過其基名與GLOB匹配的文件(使用通配符 匹配)。文件名glob可以使用*,?和[...]作爲 通配符,而\從字面上引用通配符或反斜槓字符 。

--exclude-從=(使用下 --exclude如所描述的通配符匹配)FILE 跳過的文件,其基名稱匹配任何從文件中讀取的文件名水珠 的。

--exclude-dir = DIR 從遞歸 搜索中排除與模式DIR匹配的目錄。

+1

RTFMing人是你在usenet上做的事情。 SO是不同的。順便說一句,你沒有回答這個問題。 – Constantin 2008-10-16 11:43:22

7

的grep -ir --exclude-DIR = .svn文件FOO *

在工作目錄就行了。 如果您希望搜索區分大小寫,請省略「我」。

如果你想只的.cpp檢查和.h文件使用

的grep -ir --include = {的.cpp, .H} --exclude-DIR = .svn文件FOO *

+0

是否有機會用僅匹配cpp/h的內容替換*? – Constantin 2008-10-16 11:47:50

2

去一點題外話:

如果你有大量的未跟蹤文件的工作副本(即未版本控制的),你要搜索源控制的文件,你可以做

1

我寫了this腳本,我已經添加到我的.bashrc。它會自動從grep中排除SVN目錄,查找和定位。

1

我使用這些慶典別名grepping內容和文件的svn樹...我發現它更快,更愉快的從命令行搜索(和使用vim編碼),而不是基於GUI的IDE:

s() { 
    local PATTERN=$1 
    local COLOR=$2 
    shift; shift; 
    local MOREFLAGS=$* 

    if ! test -n "$COLOR" ; then 
     # is stdout connected to terminal? 
     if test -t 1; then 
      COLOR=always 
     else 
      COLOR=none 
     fi 
    fi 

    find -L . \ 
     -not \(-name .svn -a -prune \) \ 
     -not \(-name templates_c -a -prune \) \ 
     -not \(-name log -a -prune \) \ 
     -not \(-name logs -a -prune \) \ 
     -type f \ 
     -not -name \*.swp \ 
     -not -name \*.swo \ 
     -not -name \*.obj \ 
     -not -name \*.map \ 
     -not -name access.log \ 
     -not -name \*.gif \ 
     -not -name \*.jpg \ 
     -not -name \*.png \ 
     -not -name \*.sql \ 
     -not -name \*.js \ 
     -exec grep -iIHn -E --color=${COLOR} ${MOREFLAGS} -e "${PATTERN}" \{\} \; 
} 

# s foo | less 
sl() { 
    local PATTERN=$* 
    s "$PATTERN" always | less 
} 

# like s but only lists the files that match 
smatch() { 
    local PATTERN=$1 
    s $PATTERN always -l 
} 

# recursive search (filenames) - find file 
f() { 
    find -L . -not \(-name .svn -a -prune \) \(-type f -or -type d \) -name "$1" 
}