LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-16-2017, 05:57 AM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 460

Rep: Reputation: 37
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.
 
Old 01-16-2017, 07:09 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,907
Blog Entries: 3

Rep: Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456
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
 
1 members found this post helpful.
Old 01-16-2017, 11:49 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,875

Rep: Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command --help | grep won't work vincix Linux - Newbie 7 11-17-2016 01:12 PM
grep wont work's mierdatuti Programming 6 07-30-2015 10:45 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Why won't grep work on my logs? Rob00 Linux - Newbie 11 11-04-2009 06:16 PM
how does recursive grep work? serutan Linux - Newbie 5 07-11-2008 01:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration