LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-04-2013, 07:52 AM   #1
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Rep: Reputation: 15
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?
 
Old 05-04-2013, 09:11 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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.

Last edited by towheedm; 05-04-2013 at 09:14 AM.
 
Old 05-04-2013, 10:03 AM   #3
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Original Poster
Rep: Reputation: 15
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.
 
Old 05-05-2013, 02:29 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 05-05-2013, 07:59 PM   #5
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
@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".
 
Old 05-06-2013, 03:20 AM   #6
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Perhaps something like the following would work?
http://www.physics.emory.edu/~weeks/...s/colorps.html
 
Old 05-06-2013, 03:50 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by sysbox View Post
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"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting colored text output file using C ibabhelix Programming 2 11-04-2009 02:53 AM
colored text in Solaris? dukegeo Solaris / OpenSolaris 1 04-30-2007 03:13 PM
colored text in Solaris? dukegeo Linux - Software 1 04-30-2007 11:36 AM
multi-colored unreadable text on LCD monitor gurulocu Linux - Software 3 01-28-2005 07:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration