Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a log file containing startup and shutdown times for everday.

I want to see the last pattern pattern for startup and shutdown(which is located at the end of the file being updated everyday).

Also I dont want all the lines between the startup and shutdown.I want only few(say 3) lines after the last startup and few(say 3 lines) after the last shutdown.

Any suggestions for a one liner.

share|improve this question
    
I tried like less logfile | sed -ne'/startup/,+3p' -e '/shutdown/,+3p' but it did'nt work –  munish Nov 14 '11 at 7:40

2 Answers 2

up vote 3 down vote accepted

You can use a combination of sed and grep if you don't mind to reverse the (extracted) lines of the log file twice (see How can I reverse the lines in a file?).

# test case
echo '
startup 1
shutdown 1
startup 2
shutdown 2
startup 3
line 1
line 2
line 3
line 4
line 5
shutdown 3
line 1
line 2
line 3
line 4
line 5
' | 
tail -r | sed -n -e '1,/startup/p' | tail -r | grep -E -A 3 '(startup|shutdown)'


# output
startup 3
line 1
line 2
line 3
--
shutdown 3
line 1
line 2
line 3
share|improve this answer

try to use grep instead sed wit option '--after-context'.

Example:

cat logfile | grep --after-context=3 startup
share|improve this answer
    
Useless use of cat –  ДМИТРИЙ МАЛИКОВ Nov 14 '11 at 8:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.