Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.