开发者

grep on some files

开发者 https://www.devze.com 2023-02-09 22:23 出处:网络
I want to find all 开发者_StackOverflow中文版files with extension x in all sub folders containing string s, how do I do this?

I want to find all 开发者_StackOverflow中文版files with extension x in all sub folders containing string s, how do I do this?

grep -nr s .*.x ?????

Dirk


GNU find

find . -iname "*.x" -type f -exec grep -l "s" {} +; 

If you have Ruby(1.9+)

Dir["/path/**/*.x"].each do |file|
  if test(?f,file)
   open(file).each do |line|
     if line[/s/]
       puts "file: #{file}"
       break
     end
   end
  end
end


I would first find the *.x files, and then search the string you are interested in with grep:

$ find directory -name "*.x" -exec grep -Hn s {} \;
  • -name "*.x" searches recursively every file sufixed with x.
  • -exec grep ... {} \; searchs the s string for each encountered file.
  • -H is recommended, since you wouldn't know which file matched the expression.
0

精彩评论

暂无评论...
验证码 换一张
取 消