LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2011, 01:06 PM   #1
chicone
LQ Newbie
 
Registered: May 2011
Posts: 5

Rep: Reputation: 0
bash question: searching for info and changing the color of keywords in the results


(bare with me as I am sort of new with scripting)
I am trying to figure out how to run a script that does a basic chkconfig and to get only those services that are running, but changing the color of "on" to red in my output file.

Here is what I am working with so far:

Quote:
#/bin/bash

RED=$(tput setaf 1)
BLK=$(tput setaf 0)

echo "" > /tmp/services.txt

echo "Here are the running services. Those with RL's that are turned on are in ${RED}red${BLK}:" >> /tmp/services.txt

echo "" >> /tmp/services.txt

chkconfig --list | grep <colon_symbol>on | sort >> /tmp/services
the results would return something similar to (I am only listing a couple of services for shortness) when the red is working:

Quote:
httpd 1-off 2-off 3-on 4-off 5-on 6-off
sshd 1-off 2-off 3-on 4-off 5-on 6-off
*I had to substitute a "-" and <colon_symbol> for ":" in front of the on's, because the forum thought they were smiley faces (i.e. n)

as you can see, I have no idea how to make the "on" to be red while the rest of everything remains in black text. I have been trying to read up on sed and awk, but it is still pretty much a mystery to me right now. There will be other things in the output file that I wouldn't want a rogue "on" to be in red, so just the instances of "on" in that one chkconfig return.

Thanks.
 
Old 05-05-2011, 01:28 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Filter the output thorough
Code:
sed -e 's|\([\t :-]\)\([Oo][Nn]\)\([\t ]\)|\1\x1B[0;31m\2\x1B[0m\3|g;
        s|\([\t :-]\)\([Oo][Nn]\)$|\1\x1B[0;31m\2\x1B[0m|;
        s|^\([^\t ]\+\)\([\t ]\)|\x1B[1;33m\1\x1B[0m\2|'
The \x1B[0;33m is the ANSI code for dark red, \x1B[1;33m is the ANSI code for yellow, and \x1B[0m returns to defaults.

Last edited by Nominal Animal; 05-05-2011 at 01:37 PM. Reason: Typos! And you wanted dark red, not bright red.
 
Old 05-05-2011, 03:49 PM   #3
chicone
LQ Newbie
 
Registered: May 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for posting. I tried that, but obviously messed something up. However, I did take some of what you had and googled it and found someone else who had a similar problem. This is what they came up with (I changed it very slightly to match what I needed)

sed ''/n/s//`printf "\033[31mn\033[0m"`/'g'

well, they smilies are : on (without the space in between)
 
Old 05-05-2011, 10:55 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.
Maybe this Perl script from this book could be useful too (I believe it is open source):
Code:
#!/usr/bin/perl -w
# Author calls this script rcg - Regexp Color Glasses
# Linux Server Hacks, p.209 (russian) (N 75)
use strict;
use Term::ANSIColor qw(:constants);

my %target = ();

while( my $arg = shift ) {
	my $clr = shift;

	if(($arg=~ /^-/) | (!$clr)) {
		print "Usage: rcg [regex] [color] [regex] [color] ...\n";
		exit;
	}

	$target{$arg} = eval($clr);
}

my $rst = RESET;

#  adding  of `i' at the tail of s///g
#  causes case-insensitive match
while(<>) {
	foreach my $x ( keys(%target) ) {
		s/($x)/$target{$x}$1$rst/gi;
	}
	print;
}
Example:
Code:
$ ps -Af | rcg 'gnome.*' RED 'kde.*' BLUE | less -r
 
Old 05-06-2011, 08:38 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Text does not have any color. Only the terminal or other output device understands any concept of color, and the methods used to convey color information vary from device to device. The way you apply color attributes to text on an ANSI terminal is different from the method used to color text on a Postscript file, and different from the methods used to render color attributes on other styles of terminals, and different again when displayed in a word processor or spreadsheet, and different still for every make and model of color printer.
Your question can only be answered accurately when the parameters of the output device(s) are narrowly defined.

--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash script question, searching in a variable, displaying search results... Wolfjurgen Programming 5 04-26-2011 12:29 PM
Changing color of output from bash script Fliggerty Programming 3 04-24-2010 08:40 AM
Changing Bash Color Prompt carlosinfl Red Hat 2 03-25-2010 01:16 AM
[bash] changing the color set MD3 Linux - Software 2 04-27-2005 12:38 PM
PHP: changing row color showing mysql results emilioestevezz Programming 15 09-13-2004 06:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:36 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