Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 19/20 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 27th May 2007, 10:32 AM
KClaisse Offline
Registered User
 
Join Date: May 2006
Location: Southern California
Age: 24
Posts: 291
A program to search for a string in multiple files

I have a game server that will create an average of 100 logs per day, and now totals just under 9,000 files. I need a command line program that can search through all of them for a string/regex and preferably be fast at it. Does anyone know of one?
Reply With Quote
  #2  
Old 27th May 2007, 10:37 AM
sideways Offline
Retired User
 
Join Date: Oct 2004
Location: London, UK
Posts: 4,999
grep does it all
Code:
grep -R <string> <log directory>/*
Reply With Quote
  #3  
Old 27th May 2007, 10:38 AM
KClaisse Offline
Registered User
 
Join Date: May 2006
Location: Southern California
Age: 24
Posts: 291
Quote:
Originally Posted by sideways
grep does it all
Code:
grep -R <string> <log directory>/*
I didn't know grep could do multiple files. Thanks!


EDIT: Hmm, it seems grep isn't up to the task

Code:
grep -R sometext /opt/srcds_l/srcds/cstrike/logs/*.log
-bash: /bin/grep: Argument list too long
I don't think grep can handle the magnitude of this job.

Last edited by KClaisse; 27th May 2007 at 03:48 PM.
Reply With Quote
  #4  
Old 27th May 2007, 07:44 PM
sideways Offline
Retired User
 
Join Date: Oct 2004
Location: London, UK
Posts: 4,999
Quote:
Originally Posted by KClaisse
I didn't know grep could do multiple files. Thanks!


EDIT: Hmm, it seems grep isn't up to the task

Code:
grep -R sometext /opt/srcds_l/srcds/cstrike/logs/*.log
-bash: /bin/grep: Argument list too long
I don't think grep can handle the magnitude of this job.
hmm, those logs must have strange names, use this instead

Code:
find /opt/srcds_l/srcds/cstrike/logs/ -name '*.log' | xargs grep sometext
if, you don't want to recurse subdirectories use -maxdepth 1

Code:
find /opt/srcds_l/srcds/cstrike/logs/ -maxdepth 1 -name '*.log' | xargs grep sometext
if any of the filenames have spaces/newlines or special characters then you need to use -print0

Code:
find /opt/srcds_l/srcds/cstrike/logs/ -name '*.log' -print0 | xargs -0 grep sometext
EDIT: also, try grep without the -R option, that is just to recurse subdirectories, which you may not want

Last edited by sideways; 28th May 2007 at 01:52 AM.
Reply With Quote
  #5  
Old 28th May 2007, 05:00 AM
Firewing1 Offline
Registered User
 
Join Date: Dec 2004
Location: Canada
Age: 23
Posts: 9,224
Or just do one at a time:
Code:
for i in `ls -1 logdir`;do
grep -R regex "logdir/$i"
done
Firewing1
__________________
[+] My open source software and blog
[+] Some of my howtos: (for full list, click here)
Reply With Quote
  #6  
Old 28th May 2007, 10:08 PM
A.Serbinski Offline
Registered User
 
Join Date: Jul 2006
Posts: 1,123
for FILE in *; do grep pattern $FILE; done
Reply With Quote
  #7  
Old 28th May 2007, 10:17 PM
sideways Offline
Retired User
 
Join Date: Oct 2004
Location: London, UK
Posts: 4,999
That's equivalent to

Code:
find . | xargs grep pattern
which has fewer charactes

Bothe firewing and myself posted sufficient ansewrs
Reply With Quote
  #8  
Old 28th May 2007, 11:14 PM
KClaisse Offline
Registered User
 
Join Date: May 2006
Location: Southern California
Age: 24
Posts: 291
Code:
find /opt/srcds_l/srcds/cstrike/logs/ -name '*.log' | xargs grep sometext
Works perfectly! Thanks.
Reply With Quote
Reply

Tags
files, multiple, program, search, string

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
search string in all files in a directory? tommy916 Using Fedora 2 28th June 2005 10:57 PM


Current GMT-time: 13:52 (Friday, 01-08-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat