Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using color excape codes in less

Status
Not open for further replies.

source2exe

Technical User
Sep 20, 2008
6
IL
Hi all,
Please forgive me if you see this post in other Unix forums around the web: I just don't seem to get any answers anywhere....

A. My Environment
==============
I am using RedHat Linux, version 2.6.18-53.el5.
When I type less --version I get:

less 394
Copyright (C) 1984-2005 Mark Nudelman
...

My terminal is configured as Xterm, using ANSI colors and indeed, typing
ls --color=yes produces a colored list.

B. Background
===========
I am using a small pearl script that colors lines of text according to predefined key words.
This is a very nice utility. When I redirect tail -f output to it, it produces nice color logs of my server, with words such as "Exception" or "Error" conveniently colored.

Recently I wanted to try a more ambitious use of this script: To use it together with less command.

Anyone can understand, that less +F is better than tail -f, as one can press CTRL+C at any given time when viewing a file online, and scroll or search within it effortlessly, without ever returning to the shell prompt.

Which leads me to:

C. The Problem
===========
1. I can not get less to show color.
Even typing "ls --color=yes > test.txt"
and then less -r text.txt or less -R text.txt NEVER displays the content in color. The output is actually the same as doing a "ls --color=no"

2. I read a bit about less input preprocessor the related Environment Variables in UNIX man pages : less ().

3.But, I can not figure out how I can turn less to use colors on a regular basis, How I can make less to "relay" all its output through my color pearl script, and mostly - Is it possible at all?

Any help would be highly appreciated.
 
ls --color=yes | less -R works fine for me on an RHEL 4.6 system when connected via PuTTY with TERM=xterm.

What is your terminal type and/or terminal emulator?

Annihilannic.
 
This works for me as well, probably redirecting the output to a file loses the escape sequences and this is not directly related to less. But, my problem remains:

1. How can I use less +F and pipe it somehow to a Perl script that colors the text? Less has its own prompt, input and output...
 
It sounds to me like you're trying to do it the wrong way around... shouldn't the script that colours the text should be sending its output to less -R?

Annihilannic.
 
Thanks for your replies,currently you are the only one across 5 forums who answered anything....

But, the problem is the less has its own control over input and output. Unlike tail, for example, when you break (control-c), you get to a prompt. This calls for formally "telling" less to relay its output. Use of simple redirection can not work here.

less has an input preprocessor and environment variables to support this. However:

1. I did not succeed in making in to work.
2. Less +F is special, in that it is "tailing" a file while it is being updated.

Hope I was able to make myself more clear.

Thanks
 
If you put less anywhere other than at the end of the pipeline, then it won't receive any of your keyboard commands, so you won't get the flexibility you require. I think what you need to do is tail -f whateverlog | /some/colourful/perl/script | less -R.

(I can't be bothered creating a Daniweb account, if you want to post the perl script please use something public like pastebin.)

Annihilannic.
 

I am rushing now to test this (thought about this yesterday too).
Will update about the results,
One thing, though, It is less +F if you want to "tail" a file while it is being updated. Hope this works as well.

Thanks a lot for your effort, man.
 
source2exe said:
One thing, though, It is less +F if you want to "tail" a file while it is being updated.

Yes, however in my suggestion tail is already doing that job, so less does not need to. "Follow" mode only has any relevance when reading a file; when reading stdin from a pipe it will always continue reading until the pipe is explicitly closed by the preceding programme in the pipeline.

Annihilannic.
 
OK. At last, success.
This is the only thing that is working, 100% to my satisfaction.

nohup less +F /var/log/Server.log | /usr/local/src/Colorlogs/colorlogs.pl > takethis.log &

less +F -f -R takethis.log

This works wonderfully.

Only downside is the duplication of the log files, but this can be fixed by wrapping the second less in a shell script that will delete the "takethis.log" file upon exit.

Thanks for your attention and hints!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top