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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-14-2012, 07:16 AM
|
#1
|
Member
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Rep:
|
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 ?
|
|
|
05-14-2012, 07:33 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,623
|
Code:
grep -n 'pattern' `ls -1t *.txt | head 6`
would grep in the last 6 files.
|
|
|
05-14-2012, 01:21 PM
|
#3
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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. )
|
|
|
05-14-2012, 03:45 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Something like this may work ...
Code:
stat --printf "%Y\t%n\n" * | sort -k1,1n|tail -n 5
|
|
|
05-14-2012, 07:32 PM
|
#5
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,575
|
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.
|
05-14-2012, 07:45 PM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by allend
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 ...
|
|
|
05-14-2012, 09:22 PM
|
#7
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,575
|
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'.
|
|
|
05-15-2012, 12:58 AM
|
#8
|
Member
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Original Poster
Rep:
|
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.
|
|
|
05-18-2012, 05:00 AM
|
#9
|
Member
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Original Poster
Rep:
|
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.
|
|
|
05-18-2012, 05:57 AM
|
#10
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep: 
|
Did you use the zero after print or the letter O it should be zero. As well as the -0 option.
|
|
|
All times are GMT -5. The time now is 01:22 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|