1

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

I want to see the last 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 a few (let's say 3) lines after the last startup and a few (let's say 3 lines) after the last shutdown.

Any suggestions for a one liner?

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

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
2

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

Example:

cat logfile | grep --after-context=3 startup

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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