I have a long log file where each entry begins with a line containg only hyphens.
|
feedback
|
migrated from stackoverflow.com Jul 14 '10 at 2:01
This question came from our site for professional and enthusiast programmers.
|
You can do it with a shell script thus:
Given the input file:
it produces:
By way of explanation, the
where the Then, if there was no lines with the desired pattern, it just outputs the entire file. Otherwise it deletes all the lines between 1 and the last hyphen line. As an aside, my original answer included this
However, keep in mind that it works by accumulating lines into a string and clearing the string out whenever it finds a hyphen line. Then, at the end, it simply outputs the string (all the lines after the last hyphen line). While at first glance, this may appear to be more efficient, it doesn't seem to be in reality. In (admittedly non-exhaustive) tests on my system, it actually ran quite a bit slower, I think to do with the many string appends going on. The fact is that the script solution seems to be faster despite the fact that it makes multiple passes of the data (possibly because each pass is very limited in what it does). | |||||||||||||
feedback
|
| |||||||||||
feedback
|
|
You can also do it with sed:
(with some seds, those semicolons would have to be newlines). | |||
feedback
|
|
Unfortunately, I'm not very good with sed. Hoping someone else can elaborate. EDIT OK,
| |||||||
feedback
|
| |||||
feedback
|
|
This is probably not the most efficient solution:
| |||
|
feedback
|
|
Use $ cat log-file --- first ------ second --- last $ tac log-file | sed -e '/^-\+$/,$d' | tac last | |||
|
feedback
|
But I like Norman Gray's solution much better. May like it even more if he explained it :-) | ||||
feedback
|