Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

How to print colored word in text file

by vkp (Novice)
on Jun 21, 2013 at 06:49 UTC ( #1040086=perlquestion: print w/replies, xml ) Need Help??
vkp has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How i can print a specific word say "ERROR" in red color in a text file. And i need to do this for windows and linux. Regards

Replies are listed 'Best First'.
Re: How to print colored word in text file
by Happy-the-monk (Abbot) on Jun 21, 2013 at 07:06 UTC

    red color in a text file

    Colour as in coloured text is some kind of formatting.

    Text files are understood as to contain text, but no such formatting.

    So the answer is you don't. In order to colour text, you need an output format that can colour text like some rich text format or html. Do you have something in mind already?

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: How to print colored word in text file
by RichardK (Parson) on Jun 21, 2013 at 11:05 UTC

    Term::ANSIColor will let you write coloured text to the screen. And you can store those ANSI codes in a text file, if that's any use to you?

      Have you been able to use this successfully in Windows cmd.exe? I have tried but it did not work (at least in Windows 7 that I have).

        Same problem as hdb; may be user-error, but I can't recall ever (on M$ osen) making Term::ANSIColor work as advertized.

        If you didn't program your executable by toggling in binary, it wasn't really programming!

        No, sorry but I don't run windows. I thought it worked to some limited extent on windows, but I guess I'm wrong.
Re: How to print colored word in text file
by pvaldes (Chaplain) on Jun 22, 2013 at 09:24 UTC

    I need to do this for windows and linux

    use Term::ANSIColor qw(:constants); print BOLD, RED, "ERROR!! Muahahahaha!!!\n", RESET; print "I'm fine now, thanks\n";

    Note: Term::ANSIColor comes with the perl-modules package in Debian, replacing the old libansicolor-perl package

    in a text file

    See. i.e. Latex \usepackage{color} and use perl to print the latex command to an outfile.tex, then convert outfile.tex to outfile.pdf with pdflatex

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040086]
Approved by hdb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2017-02-22 05:34 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (324 votes). Check out past polls.