LinuxQuestions.org
Visit Jeremy's Blog.
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 05-14-2012, 08:16 AM   #1
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Rep: Reputation: 21
Question How to grep faster?


I have a folder which has a couple of txt files.Now what I want is I want to grep for a particlualr pattern in say the latest 4-5 files in the folder.It must list me those files and the occurrences of the line number in those files.How can I do this ?
 
Old 05-14-2012, 08:33 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,527

Rep: Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439
Code:
grep -n 'pattern' `ls -1t *.txt | head 6`
would grep in the last 6 files.
 
Old 05-14-2012, 02:21 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
No, it's almost never a good idea to parse ls for filenames, particularly when using a command substitution. Any name it hits that has whitespace in it will be unreadable.

http://mywiki.wooledge.org/ParsingLs

Unfortunately, it's not particularly easy to safely grab "n" number of files, based on metadata like modification time, but bashFAQ 99 gives you some tips on how to do it. Doubtless google will give more.

http://mywiki.wooledge.org/BashFAQ/099

(I'd help to work out a solution myself, but unfortunately I don't have much time now. I'll come back to it later when I have more time, and if the question is still active. )
 
Old 05-14-2012, 04:45 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Something like this may work ...
Code:
stat --printf "%Y\t%n\n" * | sort -k1,1n|tail -n 5
 
Old 05-14-2012, 08:32 PM   #5
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,466

Rep: Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371
If you are trying to confine the search to recently created files then using find will do what you want.
Code:
find . -mtime -6 -print0 | xargs -0 grep -n <search string>
Run from within the directory containing your files, this will look for all files created in the last week and then pass those file names to grep. The use of xargs avoids problem with filenames containing spaces.
 
1 members found this post helpful.
Old 05-14-2012, 08:45 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by allend View Post
If you are trying to confine the search to recently created files then using find will do what you want.
Code:
find . -mtime -6 -print0 | xargs -0 grep -n <search string>
Run from within the directory containing your files, this will look for all files created in the last week and then pass those file names to grep. The use of xargs avoids problem with filenames containing spaces.
That's not the last few files, that's all files from the last 6 days ...
 
Old 05-14-2012, 10:22 PM   #7
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,466

Rep: Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371
Well, the OP did say
Quote:
I want to grep for a particular pattern in say the latest 4-5 files in the folder.It must list me those files and the occurrences of the line number in those files.
Depending on the frequency of new file creation then '-mmin' might be a better option than '-mtime'.
 
Old 05-15-2012, 01:58 AM   #8
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Smile

Hi All,
Thanks for all your responses.That helps me a lot.I am marking the thread as solved as I think I have what I need.Thanks again all of you.
 
Old 05-18-2012, 06:00 AM   #9
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Question

Sorry guys I am marking this thread as unsolved again.When I run the command given by Thinkster I am getting errors as below

find: 0652-017 -print0 is not a valid option.
xargs: The -0 flag is not valid.
Usage: xargs [-p][-t] [-e[EndOfFileString]] [-E EndOfFileString]
[-I ReplacementString] [-i[ReplacementString]] [-L Number]
[-l[Number]] [-n Number [-x]] [-s Size] [Command [Argument ...]]
The uname -a command on my server gives the follwoing output.Can you please tell what command should I run to find all the files that have the pattern "xyz" fast.
 
Old 05-18-2012, 06:57 AM   #10
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Did you use the zero after print or the letter O it should be zero. As well as the -0 option.
 
  


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
LXer: Linux 2.6.30 Gets Faster Boot - but is Fedora Faster? LXer Syndicated Linux News 0 06-11-2009 05:40 AM
LXer: Firefox 3.5 Speed Freak: Faster Development, Faster Performance LXer Syndicated Linux News 0 06-10-2009 03:42 AM
LXer: Firefox 3 Beta 4 is 5x faster than IE7, 3x faster than FF2 LXer Syndicated Linux News 0 03-12-2008 06:50 PM
Command line tool to find(1) or grep(1) faster? rsheridan6 Linux - Software 2 02-18-2006 04:50 PM
grep is faster in SUSE the REDHAT Neelesh Linux - General 7 07-25-2004 04:31 PM


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