2008-10-16 155 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" 
}