LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2603|回复: 11

怎么查找文件中的字符串

[复制链接]
发表于 2008-10-14 11:22:44 | 显示全部楼层 |阅读模式
比如要在xxx/下查找所有包含aaa的.c .h文件,xxx下有子目录
发表于 2008-10-14 12:06:42 | 显示全部楼层
  1. #!/bin/sh
  2. #注意,这儿find前和最后的引号后面均有有反单引号,用来执行这条语句
  3. #这儿的结果是一行,所以下面一个一个提取并用cat显示内容来查找是否包含"aaa"
  4. #find查找出目录以外的所有的文件类型
  5. FILE=`find ~/test/ ! -type d -print | grep "\.[ch]"`
  6. #显示所有
  7. echo ALL FILE:
  8. for i in $FILE
  9. do echo $i
  10. done
  11. #分界线,玩
  12. echo __________________________
  13. #我们想得到的
  14. echo FILES INCLUDE "aaa":
  15. for i in $FILE
  16. do
  17. #把输出"aaa"干掉,不然会有一串"aaa"在屏幕上的
  18.         if cat $i | grep "aaa" > /dev/null
  19.         then echo $i
  20.         fi
  21. done
复制代码
搂住可以自己检验,我根据以前对朋友曾经问题的答案扩展出来的。
主要是用find得到相关文件的绝对路径,然后一个一个操作,符合要求的就显示出来。
你也可以把这些输出到一个文件保存。
回复 支持 反对

使用道具 举报

发表于 2008-10-14 14:09:04 | 显示全部楼层
use the grep command and the -r option
回复 支持 反对

使用道具 举报

发表于 2008-10-14 14:52:50 | 显示全部楼层
Post by ginkgo;1893841
use the grep command and the -r option

兄弟,你试过么?
我怎么试验不出来?

grep -r aaa ~/test/*     
还是
grep -r aaa ~/test/*.[ch]
明显不对嘛

或许有其他的写法?
回复 支持 反对

使用道具 举报

发表于 2008-10-14 17:21:38 | 显示全部楼层
milo@APO~$ grep -r aaa ~/testgrep/*
/home/milo/testgrep/a:aaa
/home/milo/testgrep/a.c:aaa
/home/milo/testgrep/a.h:aaa

我在机器上的测试

grep -r 本身就是递归匹配字符串在文件夹以及子文件夹中
回复 支持 反对

使用道具 举报

发表于 2008-10-15 04:36:26 | 显示全部楼层
[]$ grep -r aaa test/*.[ch]
test/b.c:aaa
test/b.h:aaa
[]$ grep -r "aaa" test/*.[ch]
test/b.c:aaa
test/b.h:aaa
回复 支持 反对

使用道具 举报

发表于 2008-10-16 16:10:54 | 显示全部楼层
Post by asmart;1893929
milo@APO~$ grep -r aaa ~/testgrep/*
/home/milo/testgrep/a:aaa
/home/milo/testgrep/a.c:aaa
/home/milo/testgrep/a.h:aaa

我在机器上的测试

grep -r 本身就是递归匹配字符串在文件夹以及子文件夹中

呵呵,果然没看懂我的回复。
看来是没有仔细看楼主的意思,别人要求在.c .h文件中搜索的。
如果在这条后面接上awk, sed把结果处理一下,找出.c .h结尾的文件,然后打印出来,倒是也可可以的。
不过,结果的输出和显示上,最好可以更为人性化一些。
[]$ grep -r aaa test/*.[ch]
test/b.c:aaa
test/b.h:aaa
[]$ grep -r "aaa" test/*.[ch]
test/b.c:aaa
test/b.h:aaa
呵,这位兄弟,你这种命令只是针对 test/目录下面的文件的,如果test/目录下面有文件夹,那就不行了,因为你搜索的只是/test目录下面.c .h结尾的文件,而不是文件夹!
当然,如果文件夹是以.c .h结尾的话,倒是....。
回复 支持 反对

使用道具 举报

发表于 2008-10-17 13:05:57 | 显示全部楼层
  1. [ginkgo]$ grep -r aaa test/*
  2. test/b:aaa
  3. test/b.c:aaa
  4. test/b.h:aaa
  5. test/test1/b:aaa
  6. test/test1/b.h:aaa
  7. test/test1/b.c:aaa
复制代码

you are right. I make a mistake

you can use the below command  
  1. [ginkgo]$ find test -name "*.[ch]" | xargs grep aaa
  2. test/test1/b.h:aaa
  3. test/test1/b.c:aaa
  4. test/b.h:aaa
  5. test/b.c:aaa
复制代码

if you do not like the "aaa" is display append some command
  1. find test -name "*.[ch]" | xargs grep aaa| sed -e "s/:aaa$//"
复制代码
回复 支持 反对

使用道具 举报

发表于 2008-10-17 17:42:08 | 显示全部楼层
Post by ginkgo;1895159

  1. [ginkgo]$ grep -r aaa test/*
  2. test/b:aaa
  3. test/b.c:aaa
  4. test/b.h:aaa
  5. test/test1/b:aaa
  6. test/test1/b.h:aaa
  7. test/test1/b.c:aaa
复制代码


you are right. I make a mistake

you can use the below command  

  1. [ginkgo]$ find test -name "*.[ch]" | xargs grep aaa
  2. test/test1/b.h:aaa
  3. test/test1/b.c:aaa
  4. test/b.h:aaa
  5. test/b.c:aaa
复制代码


if you do not like the "aaa" is display append some command
  1. find test -name "*.[ch]" | xargs grep aaa| sed -e "s/:aaa$//"
复制代码


:cool: ;)
字数~~~~~~
回复 支持 反对

使用道具 举报

发表于 2008-10-21 23:31:49 | 显示全部楼层
grep -rl yourdir
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表