-1

I'm trying to get the last lines of a log file, and I've looked around, and found tail -f /path/to/file.log | sed '/^MYSTRING/ q' Is a common used way! BUT this one gets the data UNTIL it finds it match, not backwards.

Let me explain;

Logfile: (just example,not actual log ;)

Donald
Duck
Cat
Dog
MYSTRING
animal
fish
ape

The code above would output;

Donald
Duck
Cat
Dog
MYSTRING

but I want the last in the logfile AFTER the LAST MYSTRING (yes,there might be a few I there, that's why I need to do this backwards),

animal
fish
ape
  • how many lines do you want after `MYSTRING`? – pynexj Jan 19 '21 at 07:51
  • 2
    In general `tail -f` is applied to monior the *growing* log file. If so, we cannot determine the *last lines of a log file*. Or do you just want to print the lines *after* the specified pattern of the static (not growing) log file? – tshiono Jan 19 '21 at 07:55
  • @pynexj from the end of the file to the first MYSTRING (backwards) – Adam Larsson Jan 19 '21 at 08:05
  • @AdamLarsson : You basically want the display of the logfile to start, after MYSTRING has been encountered, and then continue to display it, **while** the process writing to the file it is still producing log output? – user1934428 Jan 19 '21 at 08:07
  • @tshiono when triggered, I want it to read from the end of the current log until it strikes the MYSTRING, it's a live log, but only written to now and then.. Determining the end of the file, isn't that what tail commad do? – Adam Larsson Jan 19 '21 at 08:08
  • @user1934428 when triggered, I want it to get everything from the last MYSTRING in the log, and all the way to the end of the log, usually 4-5 lines, the log get written to very rare (3-4 times a minute) – Adam Larsson Jan 19 '21 at 08:11
  • `tail` has no idea when the log file will stop growing. – pynexj Jan 19 '21 at 08:11
  • @pynexj no, maybe not, but it knows the end of the file when it's executed, it's in the time of execution I want the current end of the logfile. – Adam Larsson Jan 19 '21 at 08:13
  • @AdamLarsson : Please put this into your quesion, not in a comment, because it is not obvious. – user1934428 Jan 19 '21 at 08:17
  • 1
    @adam right `tail` knows the end of the file but the issue is `tail -f` does not exit when it reaches eof. instead it'll wait for the file to grow and continue printing new lines. – pynexj Jan 19 '21 at 08:19
  • @AdamLarsson : I don't think that this can simply be done using the standard commands. I would write here a tiny program/script, which reads from stdin and throws away everything, until it encounters MYSTRING. From this point, it continues reading, but writes everything to stdout. You can then pipe your tail -f into this program, or (maybe simpler) just let your program read this logfile directly. – user1934428 Jan 19 '21 at 08:19
  • Or do you want to execute something like: `tac file.log | sed '/^MYSTRING/q'`? – tshiono Jan 19 '21 at 08:26

2 Answers2

1

It sounds to me you don't expect the log file to grow at all so tail -f does not work for you. If so you can do like this:

[bash] # cat file
Donald
Duck
Cat
Dog
MYSTRING
animal
fish
ape
[bash] # awk 'flag { print } /MYSTRING/ { flag++ }' file
animal
fish
ape
[bash] #
0

This can be achieved with sed:

sed -zrn 's/(^.*MYSTRING\n)(.*$)/\2/p' file

Consume the file as a single line (-z) and split the three sections using regular expressions (-r) Substitute the line for the second section only and print the result.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18