LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Multi-Colored Text File (https://www.linuxquestions.org/questions/linux-newbie-8/multi-colored-text-file-4175460682/)

sysbox 05-04-2013 07:52 AM

Multi-Colored Text File
 
Hi. I'd like to create regular ASCII text files in Linux, but I'd also like some words to be in different colrs: red, blue, green, etc, and then print them.

Is this possible with regular text files? If not, what is the easiest way to do this?

towheedm 05-04-2013 09:11 AM

A regular text file is just that, a plain old simple text files with only alphanumeric characters.

If you would like to have certain words/lines formatted with different color attributes, you need to insert some control codes into the file.

I did this some time ago when I wanted to show my Pre-Login banner using different colors.

Here is the modified shell script with an example text file.
Code:

#! /bin/sh

# This script displays a text file with color attributes for different
# words/lines.

# The name of the text file to show is passed as the first argument
# to the script.

# ie: script.sh somefile.txt

# Copyright (C) 2013 Towheed Mohammed
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details at <http://www.gnu.org/licenses/>.

# These are the codes for the text attributes.
# [color_normal]    Set the color attributes to normal.
# [bold_on]          Set the bold attribute.
# [bold_off]        Reset the bold attribute (normal intensity).
# [reverse_on]      Set reverse video.
# [reverse_off]      Reset reverse video.
# [black]            Set text color to black.
# [red]              Set text color to red.
# [green]            Set text color to green.
# [brown]            Set the text color to brown.
# [blue]            Set the text color to blue.
# [magenta]          Set the text color to magenta.
# [cyan]            Set the text color to cyan.
# [white]            Set the text color to white.
# [black_bkg]        Set the background color to black.
# [red_bkg]          Set the background color to red.
# [green_bkg]        Set the background color to green.
# [brown_bkg]        Set the background color to brown.
# [blue_bkg]        Set the background color to blue.
# [magenta_bkg]      Set the background color to magenta.
# [cyan_bkg]        Set the background color to cyan.
# [white_bkg]        Set the background color to white.

# Color attributes variable assignment.
normal='\\033[0m'
bold_on='\\033[1m'
bold_off='\\033[22m'
reverse_on='\\033[7m'
reverse_off='\\033[27m'

# Foreground colors.
black='\\033[30m'
red='\\033[31m'
green='\\033[32m'
brown='\\033[33m'
blue='\\033[34m'
magenta='\\033[35m'
cyan='\\033[36m'
white='\\033[37m'

# Background colors
black_bkg='\\033[40m'
red_bkg='\\033[41m'
green_bkg='\\033[42m'
brown_bkg='\\033[43m'
blue_bkg='\\033[44m'
magenta_bkg='\\033[45m'
cyan_bkg='\\033[46m'
white_bkg='\\033[47m'

# Read the /etc/issue file.
echo -e "$(sed -e 's,\[color_normal],'"$normal"',g' \
-e 's,\[bold_on],'"$bold_on"',g' \
-e 's,\[bold_off],'"$bold_off"',g' \
-e 's,\[reverse_on],'"$reverse_on"',g' \
-e 's,\[reverse_off],'"$reverse_off"',g' \
-e 's,\[black],'"$black"',g' \
-e 's,\[red],'"$red"',g' \
-e 's,\[green],'"$green"',g' \
-e 's,\[brown],'"$brown"',g' \
-e 's,\[blue],'"$blue"',g' \
-e 's,\[magenta],'"$magenta"',g' \
-e 's,\[cyan],'"$cyan"',g' \
-e 's,\[white],'"$white"',g' \
-e 's,\[black_bkg],'"$black_bkg"',g' \
-e 's,\[red_bkg],'"$red_bkg"',g' \
-e 's,\[green_bkg],'"$green_bkg"',g' \
-e 's,\[brown_bkg],'"$brown_bkg"',g' \
-e 's,\[blue_bkg],'"$blue_bkg"',g' \
-e 's,\[magenta_bkg],'"$magenta_bkg"',g' \
-e 's,\[cyan_bkg],'"$cyan_bkg"',g' \
-e 's,\[white_bkg],'"$white_bkg"',g' < $1)"

exit 0

Here is a sample text file:
Code:

[blue]This is blue text. [bold_on]This is blue text that's bold.[bold_off]
[white][green_bkg]This is white text on a green background.
[color_normal]This is the default colored text on the default background color.
[red]red text [brown]brown text [green]green text

I'm not sure of the printer supports the same text attributes as the shell. If it does, then it should print the same as it's displayed on the screen.

Hope this helps.

sysbox 05-04-2013 10:03 AM

That's nice, but it doesn't help my scenario. I need to print the file, not merely display the file on the computer screen. When I print the output from your program on a real printer, it's all in black ink.

David the H. 05-05-2013 02:29 PM

You seem to be missing the point. Again, a text file on its own doesn't have that ability. It's just a series of bytes that represent text characters in some character encoding. The only colors or formatting you could get would come from some other program interpreting the data contained and displaying it in an appropriate manner.

As an example, a web page is just a text file. Open up an html source file in a regular text browser and you'll see the raw text. But that text includes html mark-up codes, and if you open it in a browser window that mark up will be interpreted and formatted into a web page. That is, only a program with an html parser built in will be able to display it the way it's intended.

Colors in the console shell work the same way, except that the mark-up is in the form of ansi escape codes. Only a program that understands the escapes will be able to show you actual colors. Any other text viewer will just display the raw codes.

So your only option is to write up and store the text in a way that allows you to use colors. I recommend using something like libreoffice. Then you can either print it directly or save it as an .odf, .doc, or .pdf, any of which can hold color formatting and allow them to be displayed in their appropriate viewers. But it won't be a plain text file after that.

towheedm 05-05-2013 07:59 PM

@OP: Several people have asked similar questions on the SuperUser and StackExchange forums.

Do a Google search for "Printing Multi-Colored Text file to lp0".

273 05-06-2013 03:20 AM

Perhaps something like the following would work?
http://www.physics.emory.edu/~weeks/...s/colorps.html

Habitual 05-06-2013 03:50 PM

Quote:

Originally Posted by sysbox (Post 4944728)
Hi. I'd like to create regular ASCII text files in Linux, but I'd also like some words to be in different colrs: red, blue, green, etc, and then print them.

Is this possible with regular text files? If not, what is the easiest way to do this?

Code:

printf "\033[2;32mHello\033[0m"


All times are GMT -5. The time now is 12:30 PM.