Like less, but with more
February 5, 2016 2:32 PM   Subscribe

I want a unix command that's basically less but I want to be able to insert something into the file as I'm monitoring it with less -F

Basically I want to monitor a log and then flag in the log when something happens externally.
posted by GuyZero to Computers & Internet (6 answers total) 2 users marked this as a favorite
 
less -F just is regular less, but it quits immediately if no scrolling is required to fit the output on the page. I think you must be think of tail -F or tail -F | less or something.

From another shell you should be able to just do :
echo "something interesting just happened" >> filename.log

You can even do that from within less; you can run any shell command by prefixing it with an exclamation mark! Once you've done it the first time, !! will repeat the past command, so that's not terrible. Unfortunately if you're piping tail through less, less doesn't know the name of the file; otherwise less would let you just use a % instead of making you type the filename.
posted by aubilenon at 2:58 PM on February 5, 2016


Response by poster: less -F is supposed to be more-or-less the same as tail -f - it works mostly the same for me - sometimes I have to hit F again. But anyways, yes.
posted by GuyZero at 3:05 PM on February 5, 2016


You could do something like
(tail -f log & cat -) | tee outfile

posted by eruonna at 3:06 PM on February 5, 2016


Best answer: Because of the way file buffering works, I don't think aubilenon's idea will work reliably. Depending on how close you need your flag to be to the line you want to mark, you may have to write your own utility.

When you say you're monitoring it, do you mean a person sitting at a terminal and watching the screen? Or will a program be grepping the log output looking for something?

If it's a person, it's actually a slightly challenging problem because one thread needs to block waiting for the person to do something to indicate that he saw something interesting, while another thread needs to read stdin (or whatever), print it and save it to the file, with any marks added by the observer.

I'm not aware of any standard utilities that do that. Maybe screen(1) with logging turned on?

(OP: Yes, screen -L does it. Run screen, then run your tail or whatever. When you type your mark or flag, screen puts everything in the right place in the logfile.)
posted by spacewrench at 3:42 PM on February 5, 2016


less -F is supposed to be more-or-less the same as tail -f - it works mostly the same for me - sometimes I have to hit F again. But anyways, yes.

Ah. From what I can tell, it's the F command that does that, and the -F command line argument is unrelated, but you can enter commands to run at startup with a + so less +F should start it up in that mode?

If you're not also using the visual editor function, you could cheat that to make "v" insert a marker in your log:
$ cat ~/mark.sh
#!/bin/sh
echo "Something interesting happened here" >> $2

$ VISUAL=~/mark.sh less +G foo.log
Then you'd have to hit ^CvF to stop monitoring, run your script on that file, and resume monitoring. If you don't want to sacrifice the visual editor for this, you could probably put together a macro using lesskey but that's not something I know anything about except that it exists.

Because of the way file buffering works, I don't think aubilenon's idea will work reliably.

That's what I thought too! But I looked around, and according to this, short appends are atomic. Of course if the log is spewing by super fast, you're not going to be able to position your markers very reliably this way.

If you actually want to insert the markers somewhere other than the end, you're going to have to make a copy of the log, because the logging process appending to the file while another process edits the file will sure make a mess of things. The first argument to the visual editor is the line offset. Another option might be to just keep track of the interesting line numbers in a separate file, and then either use them to interpose markers into your log offline, or just use them to skip around without modifying the log at all.
posted by aubilenon at 3:58 PM on February 5, 2016


If the log file is accurately timestamped I'd be tempted to just edit another file in your favourite text editor, and have a shortcut that inserts a timestamp in the same format of the log. Then ensure all your comments are timestamped... then merge the files. Or I expect you can figure out how to insert timestamps automatically in the file too if your Google-Fu is better than mine.
posted by DancingYear at 1:40 PM on February 7, 2016


« Older Kids and Cats   |   Name those tunes Newer »
This thread is closed to new comments.