LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Grep does not work (https://www.linuxquestions.org/questions/linux-general-1/grep-does-not-work-4175597565/)

manolakis 01-16-2017 05:57 AM

Grep does not work
 
Hello,

I am trying to search for a string in some xml files. For instance, I use grep like the following.
Quote:

grep -r "<name>John</name>" *
I know that this string is contained in the xml files but grep does not return any results.
Is it something that I do wrong?

Thank you,
manolakis.

Turbocapitalist 01-16-2017 07:09 AM

The XML element <name> can be spread over one or more lines, among other problems. So plain grep is not the right tool for XML.

There are a lot of tools that work with Xpaths. None of them, that I know of, really work like grep not even xgrep. Hopefully I am wrong about that.

Playing with xpath and find you can get several variations that approximate what you can get from grep. But that gets quite complicated. If you want just the list of file names then xml_grep can do the job:

Code:

xml_grep --files '//name' ./*.xml
xml_grep --files "//name[string() = 'John']" ./*.xml


pan64 01-16-2017 11:49 AM

if you really want to look for the given string you may try grep -f (see man page about that), but actually xmls cannot be handled easily without real xml parser.


All times are GMT -5. The time now is 10:54 PM.