LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-29-2013, 11:05 PM   #1
sluge
Member
 
Registered: Dec 2006
Location: Russia,52
Posts: 128

Rep: Reputation: 6
Question Strange with grep


Hello!
I executed command:

Code:
# find /var -mtime -1 | grep 'I*'
/var/cache/man/whatis
/var/lock
/var/run/utmp
/var/log/rhsm/rhsmcertd.log
/var/log/rhsm/rhsm.log
/var/log/cron
/var/log/sa
/var/log/sa/sa28
/var/log/sa/sa29
/var/log/sa/sar28
/var/log/lastlog
/var/log/messages
/var/log/audit/audit.log
/var/log/wtmp
/var/log/secure
So, I hope that grep will output all lines that started from I, but it output all strings without filtering, why?
 
Old 01-30-2013, 12:28 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Because grep 'I*' means:
show everything with zero or more 'I' in it
I is obvious that every string has zero 'I' in it.
If you need lines starting with 'I' you'd need grep ^I

jlinkels
 
Old 01-30-2013, 02:43 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,152

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Communication problem maybe - all those strings are going to start with "/var".
Grep 'I.*' may do what you want, maybe "grep '\/I', maybe something else.
 
Old 01-31-2013, 02:58 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
How about just using find the way it's meant to be used, instead of going through the extra step of filtering it with grep? Add a -name or -path option with a properly-crafted globbing pattern in it to filter out the names you want.

Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

I'd give you an actual example, but to repeat syg00, the actual requirements are unclear. The above output doesn't have any 'I's in it at all.

Edit:

PS: If perhaps you're trying to find that pattern inside the files, you'd have to run grep in an -exec option. The grep command in the OP filters the list as text input, not as files to search.

How can I recursively search all files for a string?
http://mywiki.wooledge.org/BashFAQ/008

Last edited by David the H.; 01-31-2013 at 03:05 PM.
 
Old 01-31-2013, 04:52 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by David the H. View Post
How about just using find the way it's meant to be used, instead of going through the extra step of filtering it with grep? Add a -name or -path option with a properly-crafted globbing pattern in it to filter out the names you want.
Interesting. I interpreted what to OP wanted as something completely different, although your interpretation seems quite reasonable. My interpretation was that the OP wants to use the list of files found by find as the set of files in which grep should scan for the (incorrectly) specified pattern. Searching for filespecs that start with 'I' when the filespec, by definition, is always '/var/....' is completely nonsensical.

If my interpretation is correct, then at least two solutions exist:
Code:
grep '^I' $(find /var -mtime -1)
#
#  Or...
#
find /var -mtime -1 -exec grep '^I' {} \;
--- rod.

Last edited by theNbomr; 01-31-2013 at 04:53 PM.
 
Old 02-01-2013, 05:16 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yeah, I belatedly realized that that may have been his desire, which is why I went pack to edit my post. In either case my point on the correct use of find is the same.


To add a bit of further clarification, "|" pipes send the stdout of the first command, that is, the text that usually prints on screen, into the stdin of the second command. How that command handles the input depends on it's design, but most text editing commands like grep just process it as-is, as text. Only a small number of commands such as xargs actually read them as filenames (and find's -exec option can usually replace xargs).


Another way to process a list of filenames is to use a shell while loop, particularly if you need to do complex operations on them :

Code:
while IFS='' read -r -d '' fname; do

	grep 'string' "$fname"

done < <( find. -type f -mtime -1 -print0 )
You should always use null separators (-print0) when the exact input patterns are unknown.

If the file matching depends only on name patterns, and not mtime or such, then you can also just use grep's built-in recursive file processing.

Code:
grep -r --include='*.txt' -e '^I' ./
This will search all textfiles for the given pattern.

All this is covered in the links I gave above.
 
  


Reply



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
[SOLVED] grep -o and strange character di80 Linux - Newbie 4 10-16-2012 06:19 AM
ps | grep command combination - Strange Output. Why Environment Variables Listed ? nkataria Linux - General 1 04-07-2010 03:50 AM
Strange error using grep in a while loop anil3 Linux - General 4 03-30-2009 03:09 AM
strange behavior of find or grep alenD Programming 4 09-21-2007 06:35 AM
grep acting strange in script fredgt Programming 2 12-14-2004 11:15 AM

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

All times are GMT -5. The time now is 05:39 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