How to search for text in specific files in unix

I am using an Ubuntu machine and am trying to run the following commands to search for text:

This is the command to check if a word is recursively present in a given directory:

1) Here <hello>

is the word I am looking for and it searches recursively in all files starting from the current directory. It works fine.

grep -r "<hello>" .

2) Now I want to limit the search to only specific files, let's say only in files xml

:

grep --include=\*.{java} -rnw '/home/myfolder/' -e "<hello>"

      

This time, the team takes longer and finally does not produce any results. But my files have content.

I followed this link - How do I find all files containing specific text in Linux? for writing my second command.

Is there a problem with my second command? Also is there an alternative command that executes quickly?

+3


source to share


3 answers


Better to use find

as grep

include / exclude can get a little confusing:

find -type f -name "*.xml" -exec grep -l 'hello' {} +

      

It looks for files whose name ends with .xml

and executes on it grep 'hello'

. With -l

(L) we create a filename to print without a matched string.



Explanation

  • find -type f

    it finds the files in the given directory structure.
  • -name "*.xml"

    selects those files whose name ends in .xml

    .
  • -exec

    execute the command for each command result find

    .
  • -exec grep -l 'hello' {} +

    execute grep -l 'hello'

    in this file. With, {} +

    we are referencing the matching name (this looks like an execution grep 'hello' file

    , but referencing the name of the file provided by the command find

    ). In addition, grep -l

    (L) returns the filename, not the match.
+4


source


So the problem is that - XML

- not plain text, no matter how it looks. Therefore, it is not suitable for "normal" grepping.

Can I have a look at [ xml_grep][1]

which is the utility provided with the package XML::Twig

for this purpose?



Or, if you can give more specific examples of what the original content is and what the outputs are, we can provide more specific answers.

Anyway, other than that, I wouldn't do a recursive grep, but rather find -exec

. find

allows you to filter the files first and is efficient enough ... but there is really no way around the fact that you will need to read every file that matches the check.

0


source


This works for me looking for files *.xml

and *.java

using GNU grep

:

grep --include=\*.{xml,java} -rl '/path' -e 'hello'

      

In your question, you had -w

like a flag, which means the whole word match.

0


source







All Articles