How do I color some words in output?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I color some words in output?
# 1  
Old 03-22-2011
How do I color some words in output?

OK, let's say if we want to filter lines in man and get a different color for the word we are searching we write man find | grep print
It will mark all the words containing print red and output only those lines containing it, let's say I want to see the complete manual, but with marked words containing print? Like grep wouldn't throw out the lines that don't have "print", but it would only mark them red let's say?
# 2  
Old 03-22-2011
Hi.

Here's one solution with a utility glark:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate colorized text from glark.
# http://www.incava.org/projects/glark/

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C glark

FILE=${1-data1}
pl " Data file $FILE:"
cat $FILE

pl " Results:"
glark --no-filter -o is the $FILE

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39
glark, version 1.8.0

-----
 Data file data1:
Now is the time
for all good men
to come to the aid
of their country.

-----
 Results:
    1 Now is the time
    2 for all good men
    3 to come to the aid
    4 of their country.

See text file f1.txt for the results displayed in color at your terminal -- the color codes are not easily copy-pasted from a terminal into forums.

The glark code was in the GNU/Linux Debian repository I use. If it is not in yours, see the web page noted in the script above.

Good luck ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cell color based on Status in HTML output

Hi, I need to get the Status Column in Green if it is approved and Red if it is declined in the HTML output attachment#!/bin/bash body_csv="/authlistener/ProdA/service/queryRS.csv" body_html="/authlistener/ProdA/service/queryRS.html" ... (1 Reply)
Discussion started by: maddelav
1 Replies

2. Shell Programming and Scripting

Color not showing in output text when sent via email

Hi, I have a test script where I am trying to make a line color in RED. It works when I execute the command from command line but when I try to send the output of the script via mailx it will not show me the color and instead show this output: Command to send script output to email: ... (1 Reply)
Discussion started by: tkhan9
1 Replies

3. Emergency UNIX and Linux Support

Disk space script output in color

I'm connecting to 15 servers in using ssh and storing disk space details of each server in a text file Finally , I'm emailing that text file at the particular id using mail -x . The report looks something like this. Filesystem size used avail capacity Mounted on /proc ... (30 Replies)
Discussion started by: ajaypatil_am
30 Replies

4. Shell Programming and Scripting

Append color in shell script for output

Hi Experts, I want to get my shell script output in a color for a particular word. PFB my output. TT.QM.JTV1S1 TLORSBT2.JMR701T1.C1 REPOS TT.QM.JTV1R1 TLORSBF2.JMR701T1.C1 NORMAL whenever REPOS word comes then entire line should come in red color. Can you please help me... (4 Replies)
Discussion started by: darling
4 Replies

5. Shell Programming and Scripting

putting color on output file script

do you have any simple script on how to change the color and font of a string in a script example echo "====================================" echo " sample color script" echo "====================================" echo " hello " echo " bye" on hello,... (3 Replies)
Discussion started by: lhareigh890
3 Replies

6. Shell Programming and Scripting

Color on echo output does not work

Hi, When I run: echo "\033I see hi in color, but if I run this color is not shown, why? (echo "\033Thanks Israel. ---------- Post updated at 05:17 AM ---------- Previous update was at 04:43 AM ---------- DONE!! I had to run more -v. Thanks (4 Replies)
Discussion started by: iga3725
4 Replies

7. Shell Programming and Scripting

give some color to a word on echo output

Hi Supposed this text output: How can I show $HOSTNAME in other color than blank? :-) KSH on AIX (4 Replies)
Discussion started by: iga3725
4 Replies

8. Shell Programming and Scripting

.bashrc/PS1 command color different color to command output

I have been configuring my .bashrc PS1 to be displayed with some nice colors and in a format that I like, however there is one thing that I cannot figure out (or know if it's even possible). my PS1 line is as follows: export PS1='\\u\@\\h\:\\w\n\\$ \'This makes the command and command output... (0 Replies)
Discussion started by: jelloir
0 Replies
Login or Register to Ask a Question