LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 02-06-2005, 09:25 PM   #1
dmellem
Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 42

Rep: Reputation: 15
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
 
Old 02-06-2005, 10:56 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
Code:
ls access* | sort -n | xargs zgrep pattern

Last edited by Berhanie; 02-06-2005 at 10:58 PM.
 
Old 02-06-2005, 11:57 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
If you haven't messed with the file's timestamps, there's always
ls -t access*| grep <whatever>
 
Old 02-07-2005, 12:01 AM   #4
dmellem
Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 42

Original Poster
Rep: Reputation: 15
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
 
Old 02-07-2005, 12:22 AM   #5
dmellem
Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 42

Original Poster
Rep: Reputation: 15
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
 
Old 02-07-2005, 01:17 AM   #6
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
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.
 
Old 02-11-2005, 01:05 PM   #7
dmellem
Member
 
Registered: Mar 2003
Distribution: Fedora
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks. With a minor change:
Code:
ls access* |sort -nr -t . -k 3,3 | xargs zgrep pattern
it worked.

-Dan
 
  


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
finding considered log name during logrotate Matt Collier Linux - Software 1 10-03-2005 07:51 AM
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 02:00 PM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 10:06 AM
Bash - Strip Spaces from string then cat cmfarley19 Programming 8 07-25-2004 12:01 PM
How to add my log files in logrotate Groucho Slackware 2 10-03-2003 08:16 AM


All times are GMT -5. The time now is 11:52 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration