Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Using SED to change a specific word's color? Post 302415114 by pseudocoder on Wednesday 21st of April 2010 08:39:12 PM
Old 04-21-2010
I think your requirement is too complex to be done with an one-liner.
Here is a fully working script which can search up to 3 words.
Feel free to modify it if you need to search your logfile(s) for more than 3 words.

Code:
#!/bin/sh

if [ $# -eq 0 ] ; then
	
        echo " usage: $0 logfile arg1 arg2 arg3"
        echo " arg2 and arg3 are optional"
        exit 1

elif [ ! -e $1 ] ; then

        echo " file $1 does not exist"
	exit 1

elif [ "$2" = "" ] ; then

        echo " no arg1"
	exit 1

elif [ $# -gt 4 ] ; then

	echo " too many arguments!"
        exit 1

elif [ $# -eq 4 ] ; then

	cat $1 | sed -e ''/$2/s//`printf "\033[32m$2\033[0m"`/'' \
                     -e ''/$3/s//`printf "\033[32m$3\033[0m"`/'' \
		     -e ''/$4/s//`printf "\033[32m$4\033[0m"`/''
	exit 0

elif [ $# -eq 3 ] ; then

 	cat $1 | sed -e ''/$2/s//`printf "\033[32m$2\033[0m"`/'' \
		     -e ''/$3/s//`printf "\033[32m$3\033[0m"`/''
	exit 0

elif [ $# -eq 2 ] ; then

        cat $1 | sed -e ''/$2/s//`printf "\033[32m$2\033[0m"`/''
	exit 0

fi

Code:
$ ./sgrep 
 usage: ./sgrep logfile arg1 arg2 arg3
 arg2 and arg3 are optional
$ ./sgrep mylogg
 file mylogg does not exist
$ ./sgrep mylog
 no arg1
$ ./sgrep mylog LINE
LINE 001 START 00:01 END 00:11
LINE 002 START 01:01 END 01:11
LINE 003 START 02:01 END 02:11
LINE 004 START 03:01 END 03:11
LINE 005 START 04:01 END 04:11
$ ./sgrep mylog LINE START
LINE 001 START 00:01 END 00:11
LINE 002 START 01:01 END 01:11
LINE 003 START 02:01 END 02:11
LINE 004 START 03:01 END 03:11
LINE 005 START 04:01 END 04:11
$ ./sgrep mylog LINE START END
LINE 001 START 00:01 END 00:11
LINE 002 START 01:01 END 01:11
LINE 003 START 02:01 END 02:11
LINE 004 START 03:01 END 03:11
LINE 005 START 04:01 END 04:11
$ ./sgrep mylog LINE START END WTF
 too many arguments!
$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies

2. Shell Programming and Scripting

Change specific occurence with sed

Hello, I have this file. aaa port=1234 time bbb port=2233 name ccc port=4444 name Is there any way with sed to change only the occurence of "port" which comes after section to have as output : (12 Replies)
Discussion started by: rany1
12 Replies

3. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

4. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

5. Shell Programming and Scripting

sed / awk to get specific word in line

I have http log that I want to get words after specific "tag", this a sample line from the log: 98,POST,200 OK,www.facebook.com,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1,/ajax/updatestatus.php?__a=1,datr=P_H1TgjTczCHxiGwdIF5tvpC; lu=Si1fMkcrU2SInpY8tk_7tAnw;... (6 Replies)
Discussion started by: erlanq
6 Replies

6. UNIX for Dummies Questions & Answers

How to change the background color in the init 3 mode(not line color)

Hello, I am using RHEL 6.1 on VMware I am searching for a way to change background color (not line by line color wich one can using tput command) basically changing the color of the whole screen to white instead of the default black and changing font color to black and alos would like to... (2 Replies)
Discussion started by: Dexobox
2 Replies

7. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

8. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

9. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

10. Shell Programming and Scripting

Using sed to change values after a specific string

Hello I have a script that searches a file for a specific string and then changes the nth column after that string. I have searched online for how to do this with sed but have not seemed to find a solution that works for me. I am using bash. Some background info: - Currently I am using awk to... (4 Replies)
Discussion started by: prodigious8
4 Replies
DXmColorMixGetNewColor(3X)												DXmColorMixGetNewColor(3X)

NAME
DXmColorMixGetNewColor - Retrieves (returns) the color mixing widget's current new color red, green, and blue values. SYNOPSIS
void DXmColorMixGetNewColor( DXmColorMixWidget cmw, unsigned short *red, unsigned short *green, unsigned short *blue ); PARAMETERS
The widget identifier of the color mixing widget. A pointer to the returned new color red value. A pointer to the returned new color green value. A pointer to the returned new color blue value. A complete list of all the X color values available to you is located on your system. To find it, type the following command: % find / -name '*rgb*' -print DESCRIPTION
DXmColorMixGetNewColor allows the calling application or routine to quickly obtain the current color value from the color mixing widget. Note that if your application uses the default color mixing subwidget, the application will perform this operation faster if you call this routine instead of the Intrinsic routine XtGetValues. SEE ALSO
DXmColorMixSetNewColor(3X), DXmCreateColorMix(3X), DXmCreateColorMixDialog(3X) DXmColorMixGetNewColor(3X)
All times are GMT -4. The time now is 01:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy