LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-17-2013, 04:28 PM   #1
prasunjit
LQ Newbie
 
Registered: Dec 2012
Posts: 17

Rep: Reputation: Disabled
problem in grep command


Hi All.

Can anyone please clarify the difference between the below commands

I am trying to find files with the below command. the expr 20349234330 is present in a group of two xml files. When i throw below command i get following error
find . -type f -print | xargs grep -l 20349234330
grep: ./CFDataFromVendor_388112097_20130117140311: No such file or directory
grep: .xml: No such file or directory
grep: ./CFDataFromVendor_388102746_20130117135611: No such file or directory
grep: .xml: No such file or directory

But when i throw below command i am able to find the exact file
grep -l '20349234330' *.xml
CFDataFromVendor_388112097_20130117140311.xml

is the grep command not able to get argument list from the piped xml files.?

The command find . -type f -print | xargs grep -l works fine if i am trying to match the expression from a group of .txt files.

Last edited by prasunjit; 01-17-2013 at 04:30 PM.
 
Old 01-17-2013, 04:36 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
it looks like your find statement is breaking up the the file name into two seperate arguments. Can you confirm that by changing your xargs to an echo and see if it is? That will help is narrow it down.
 
1 members found this post helpful.
Old 01-17-2013, 05:29 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
How about:
Code:
find . -type f -print0 | xargs -0 grep -l 20349234330
 
1 members found this post helpful.
Old 01-17-2013, 05:47 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
the -0 is a nifty trick, completely forgot about it. You can also use -e to specify the end of file on an argument.
 
Old 01-18-2013, 10:49 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
1) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Code:
grep: ./CFDataFromVendor_388112097_20130117140311: No such file or directory
grep: .xml: No such file or directory
These two error lines indicate that the filename has been treated as two separate entries, which means that word-splitting has taken place. There appears to be a space before the ".xml", or perhaps your IFS variable has been set to period or something. That's why you need to use null separators when you pass the names to xargs.

But why not just take advantage of find's built-in -exec option instead?

Code:
find . -type f -exec grep -l -- 20349234330 '{}' +
Or since you don't seem to be doing anything with find other than simple matching, you could just use grep's recursive searching feature.

Code:
grep -r -l -- 20349234330 .
You can limit it to xml files only by adding an --include option. (--include='*.xml')

How can I recursively search all files for a string?
http://mywiki.wooledge.org/BashFAQ/008
 
1 members found this post helpful.
Old 01-18-2013, 06:18 PM   #6
prasunjit
LQ Newbie
 
Registered: Dec 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thanks a lot everybody . Yes i found that there is a space before ".xml" and so my command was behaving diffrently.

Thanks for reminding me the correct way to post a question. I would definitely maintain the format when i post any question in future.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in using grep command !!! apanimesh061 Programming 5 09-26-2011 02:29 AM
Grep command problem~ Bleek Linux - Newbie 7 12-18-2009 10:49 AM
Problem with simple grep command jacksmash Linux - General 3 09-10-2008 06:01 PM
problem with grep command thodo Linux - Newbie 1 10-25-2007 05:20 AM
Simple grep command problem acis Linux - General 2 07-16-2004 01:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:53 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration