Using ‘less’ command with special character interpretation

Using ‘less’ command with special character interpretation

It is possible to use perl or bash for ascii colorization of output and send them to a log file. The advantage is that you can highlight errors, info messages etc in a log and view them later. The obvious disadvantage is that if your log file runs to a few thousand Kilobytes or more, you have to use the less command and the results are less than awesome.

For example,

A command like this:

tail -n 50 /root/wordpress_update.log

Normally results in:

MWSnap004 2013-04-02, 09_24_36

 

If less was used for piping:

tail -n 50 /root/wordpress_update.log | less

It now looks like this:

MWSnap005 2013-04-02, 09_27_21

Which is very ugly.

The solution is to use the -R switch in less:

tail -n 50 /root/wordpress_update.log | less -R

Which results in what you needed:

MWSnap006 2013-04-02, 09_29_23


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.