LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   text processing, maintaining a log (https://www.linuxquestions.org/questions/linux-newbie-8/text-processing-maintaining-a-log-356433/)

jonhewer 08-24-2005 06:48 AM

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

Simon Bridge 08-24-2005 07:07 AM

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.

jonhewer 08-24-2005 07:10 AM

i'll have a read, thanks

jonhewer 08-24-2005 08:40 AM

erm....any more hints? not sure how i can grab the 10th last blank line onwards .... :confused:

bathory 08-24-2005 09:20 AM

To view the 20 last entries of your logfile:
Code:

tail -n 20 logfile

theYinYeti 08-24-2005 09:34 AM

Code:

tac logfile | awk '/^$/ {N++}; N<11 {print $0}' | tac
Yves.

geeman2.0 08-24-2005 09:43 AM

You could periodically do something like this:
Code:

tail log.txt > tmp.txt
cat tmp.txt > log.txt


jonhewer 08-24-2005 10:19 AM

thanks very much YinYeti, that's perfect :)

Simon Bridge 08-24-2005 11:16 PM

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 :)

theYinYeti 08-25-2005 03:24 AM

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.


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