Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
02-06-2005, 09:25 PM
#1
Member
Registered: Mar 2003
Distribution: Fedora
Posts: 42
Rep:
logrotate log files - keeping in order when using grep or cat and bash
Hi,
I'm trying to sort through some Squid log files that have been rotated with logrotate. These files are named
access.log
access.log.1.gz
access.log.2.gz
.
.
.
access.log.28.gz
The oldest file is #28 and the newest file is access.log. The individual files are from oldest on the top to newest.
If I'd like to sort through all of these files, I would do something like:
zgrep 'pattern' access.log*
but the order isn't too useful. I've tried
zgrep 'pattern' access.log.[1-2][0-9].gz access.log.[1-9].gz access.log
but the pattern is 10-28,1-9,access.log instead of 28-1,access.log. I've tried writing the order backwards (i.e., [2-1][9-0]) but that doesn't work.
How can I get this to ouput correctly? Do I need to use the 'find' statement or create a shell script of some sort? I've played with
for file in $(ls access.log.[1-9].gz access.log.[1-3][0-9].gz); do echo $file; done
but that doesn't seem to help either.
Thanks,
-Dan
02-06-2005, 10:56 PM
#2
Senior Member
Registered: Dec 2003
Distribution: Fedora
Posts: 1,492
Rep:
Code:
ls access* | sort -n | xargs zgrep pattern
Last edited by Berhanie; 02-06-2005 at 10:58 PM .
02-06-2005, 11:57 PM
#3
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740
If you haven't messed with the file's timestamps, there's always
ls -t access*| grep <whatever>
02-07-2005, 12:01 AM
#4
Member
Registered: Mar 2003
Distribution: Fedora
Posts: 42
Original Poster
Rep:
Thanks for the reply. However, that doesn't seem to do it. The sort doesn't sort it in reverse numeric form. The command
ls access* | sort -n
results in:
access.log
access.log.10.gz
access.log.11.gz
access.log.12.gz
access.log.13.gz
access.log.14.gz
access.log.15.gz
access.log.16.gz
access.log.17.gz
access.log.18.gz
access.log.19.gz
access.log.1.gz
access.log.20.gz
access.log.21.gz
access.log.22.gz
access.log.23.gz
access.log.24.gz
access.log.25.gz
access.log.26.gz
access.log.27.gz
access.log.28.gz
access.log.2.gz
access.log.3.gz
access.log.4.gz
access.log.5.gz
access.log.6.gz
access.log.7.gz
access.log.8.gz
access.log.9.gz
Using 'sort -rn' just reverses this list but doesn't group it from highest number to lowest.
Thanks.
-Dan
02-07-2005, 12:22 AM
#5
Member
Registered: Mar 2003
Distribution: Fedora
Posts: 42
Original Poster
Rep:
Quote:
Originally posted by chrism01
If you haven't messed with the file's timestamps, there's always
ls -t access*| grep <whatever>
Ah, that's true. Thanks.
If you think of another solution, I'd appreciate that as well.
Thanks again,
-Dan
02-07-2005, 01:17 AM
#6
Senior Member
Registered: Dec 2003
Distribution: Fedora
Posts: 1,492
Rep:
Sorry. A little more complicated than I thought. Try this:
Code:
ls access* |sort -n -t . -k 3,3 | xargs zgrep pattern
Last edited by Berhanie; 02-07-2005 at 01:19 AM .
02-11-2005, 01:05 PM
#7
Member
Registered: Mar 2003
Distribution: Fedora
Posts: 42
Original Poster
Rep:
Thanks. With a minor change:
Code:
ls access* |sort -nr -t . -k 3,3 | xargs zgrep pattern
it worked.
-Dan
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 01:03 PM .
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