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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-24-2005, 05:48 AM
|
#1
|
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Rep:
|
text processing, maintaining a log
hi
my shell script is maintaining a log file, with log entries consisting of a varying number of lines, but always seperared from one another with a blank line
i only really need to keep, say, the last 20 entires in the log - does anyone know a command i can use which will do this, presumably using the fact that log entires are seperated by blank lines
hope that makes sense?
cheers
|
|
|
|
08-24-2005, 06:07 AM
|
#2
|
|
Guru
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211
Rep: 
|
Here's a list for you to read the man pages of:
cat, cut, dd, diff, fold, grep, nl, patch, sed, sort, split, tac, tail, tr, wc
grep and sed will probably help the most.
You want to count the blank lines = N, then copy out everything from N-10 onwards. Or you could reverse the line order, delete everything after the tenth blank line, the re-reverese the file.
|
|
|
|
08-24-2005, 06:10 AM
|
#3
|
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
i'll have a read, thanks
|
|
|
|
08-24-2005, 07:40 AM
|
#4
|
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
erm....any more hints? not sure how i can grab the 10th last blank line onwards .... 
|
|
|
|
08-24-2005, 08:20 AM
|
#5
|
|
Guru
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,393
|
To view the 20 last entries of your logfile:
|
|
|
|
08-24-2005, 08:34 AM
|
#6
|
|
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897
Rep:
|
Code:
tac logfile | awk '/^$/ {N++}; N<11 {print $0}' | tac
Yves.
|
|
|
|
08-24-2005, 08:43 AM
|
#7
|
|
Member
Registered: Feb 2005
Location: Ontario, Canada
Distribution: Gentoo, Slackware
Posts: 345
Rep:
|
You could periodically do something like this:
Code:
tail log.txt > tmp.txt
cat tmp.txt > log.txt
|
|
|
|
08-24-2005, 09:19 AM
|
#8
|
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
thanks very much YinYeti, that's perfect 
|
|
|
|
08-24-2005, 10:16 PM
|
#9
|
|
Guru
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211
Rep: 
|
bathory and geeman2.0: nice try - would be fine if there was one line per entry, but there are multiple lines per entry... each multiple-line entry sep'd by a blank line. Now you see why tail wouldn't do it.
theYinYeti: well done - I missed out awk didn't I 
|
|
|
|
08-25-2005, 02:24 AM
|
#10
|
|
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897
Rep:
|
Thanks SimonBridge
Looking back on it, I see that the tip I could have given is the necessity of counting.
Unless you manage some sort of 10-blocks long FIFO, you have to decide when you start counting (and displaying), and when you stop. This can only be done from the tail end, hence the tac.
Next, the counter had to be placed in a variable, because a simple line count did not apply here. And variables are unknown to common text processing tools, except to awk, hence the awk.
Now the output only had to be put in the proper order again, hence the final tac.
Only logic, you see.
Yves.
|
|
|
|
| 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 09: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
|
|