Is it possible to output the contents of more than one stream into separate columns in the terminal?

StackOverflow https://stackoverflow.com/questions/12958313

  •  08-07-2021
  •  | 
  •  

Question

For work, I occasionally need to monitor the output logs of services I create. These logs are short lived, and contain a lot of information that I don't necessarily need. Up until this point I've been watching them using:

grep <tag> * | less

where <tag> is either INFO, DEBUG, WARN, or ERROR. There are about 10x as many warns as there are errors, and 10x as many debugs as warns, and so forth. It makes it difficult to catch one ERROR in a sea of relevant DEBUG messages. I would like a way to, for instance, make all 'WARN' messages appear on the left-hand side of the terminal, and all the 'ERROR' messages appear on the right-hand side.

I have tried using tmux and screen, but it doesn't seem to be working on my dev machine.

Was it helpful?

Solution

Try doing this :

FILE=filename.log
vim -O <(grep 'ERR' "$FILE") <(grep 'WARN' "$FILE")

OTHER TIPS

Just use sed to indent the desired lines. Or, use colors. For example, to make ERRORS red, you could do:

$ r=$( printf '\033[1;31m' )  # escape sequence may change depending on the display
$ g=$( printf '\033[1;32m' )
$ echo $g  # Set the output color to the default
$ sed "/ERROR/ { s/^/$r/; s/$/$g/; }" *

If these are live logs, how about running these two commands in separate terminals:

Errors:

tail -f * | grep ERROR

Warnings:

tail -f * | grep WARN

Edit

To automate this you could start it in a tmux session. I tend to do this with a tmux script similar to what I described here.

In you case the script file could contain something like this:

monitor.tmux

send-keys "tail -f * | grep ERROR\n"
split
send-keys "tail -f * | grep WARN\n"

Then run like this:

tmux new -d \; source-file monitor.tmux; tmux attach

You could do this using screen. Simply split the screen vertically and run tail -f LOGFILE | grep KEYWORD on each pane.

As a shortcut, you can use the following rc file:

split -v
screen bash -c "tail -f /var/log/syslog | grep ERR"
focus
screen bash -c "tail -f /var/log/syslog | grep WARN"

then launch your screen instance using:

screen -c monitor_log_screen.rc

You can of course extend this concept much further by making more splits and use commands like tail -f and watch to get live updates of different output.

Do also explore screen other screen features such as use of multiple windows (with monitoring) and hardstatus and you can come up with quite a comprehensive "monitoring console".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top