Using SED to change a specific word's color?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using SED to change a specific word's color?
# 1  
Old 04-20-2010
Using SED to change a specific word's color?

Ok so all i'm trying to do here is output a file and change the color of a specific word. I can't use grep with color because I need all lines of the file not just lines that match the pattern.

I can get this substitution to work but when it displays it shows exactly what i'm putting it rather than changing the color.

I've tried dozens of different combinations and ways of doing this and i'm sure i'm just missing something stupid.
Code:
sed 's/START/echo -e "\033[32m" START/p' /tmp/logfile.log

All I get are log files output with START replaced with echo -e "\033[32m" START


Tried using TPUT as well by doing things like bold=$(tput smso)

Many variations of that. I never got tput to work at all even using just simple test echo with the bold variable.

Last edited by Yogesh Sawant; 04-21-2010 at 03:57 AM.. Reason: added code tags
# 2  
Old 04-20-2010
Maybe this is what you're looking for:
Code:
$ mkdir testcolor
$ cd testcolor
$ echo "LINE 001 START 00:01 END 00:11" > mylog
$ echo "LINE 002 START 01:01 END 01:11" >> mylog
$ echo "LINE 003 START 02:01 END 02:11" >> mylog
$ echo "LINE 004 START 03:01 END 03:11" >> mylog
$ echo "LINE 005 START 04:01 END 04:11" >> mylog
$ cat mylog
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
$ cat mylog | sed ''/START/s//`printf "\033[32mSTART\033[0m"`/'' > mylog
$ cat mylog
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
$

# 3  
Old 04-20-2010
Yes that works thank you very much. I didn't output the results to a file though I just had it present them on the screen,

Now what I'm going to try and do is save the command as an alias so that I can cat any file and pipe it into the alias and it will do this for me.

Only thing I'm having difficulty with is getting it to ask me for the variable and use that in the SED command. I can't think of any way I could do it by doing a simple

Code:
cat file | alias

So I may have to just a straight up alias that asks me for 2 variables. the file and word. Not as easy to slip in but you do what you can I guess.

I have the command working if I specify the variable ahead of time. But ideally the word(s) I will want is going to vary all the time.

---------- Post updated at 09:10 PM ---------- Previous update was at 09:07 PM ----------

Can someone explain why the printf command is enclosed in ` and not a single quote ' ? What is the difference there. I mean it doesn't work if I use single quotes and the other character I don't even know specifically what that is called. I've never used it in any other command i've written.

Last edited by Scott; 04-21-2010 at 07:38 PM.. Reason: Please use code tags
# 4  
Old 04-20-2010
Quote:
Originally Posted by MrEddy
Can someone explain why the printf command is enclosed in ` and not a single quote ' ?
from UNIX in a nutshell:
Code:
` `	Substitute output of enclosed command.

You could use $() instead, like:
Code:
$ cat mylog | sed ''/START/s//$(printf "\033[32mSTART\033[0m")/''

Sorry, I have no idea how to get your alias thing working.
# 5  
Old 04-21-2010
Well this is what I have so far. I use the following to actually exceute the highlight word search. So I alias sgrep to this command
Code:
alias sgrep ='echo highlightword? ; read highlightword ; cat $pathoffile | sed ''/$highlightword/s//`printf "\033[32m$highlightword\033[0m"`/'''

This works fine if I specify the pathoffile variable ahead of time. I've been trying to figure out a way to get that variable set with one line. I've triedt his.

alias scat ='pathoffile='

So that in actual use I could do scat /var/log/logfile | sgrep

Then it would substitute in the path to the file and set it as the variable pathoffile but not having much luck there so far.

---------- Post updated at 10:28 PM ---------- Previous update was at 10:12 PM ----------

I wonder if it would be easier to just have it change the color of an entire line that has a word matching the pattern? Rather than just changing the color of the one word.

Last edited by Yogesh Sawant; 04-21-2010 at 03:58 AM.. Reason: added code tags
# 6  
Old 04-21-2010
I've played a bit with that alias thing, but it didn't work properly for me.
Instead I've created a small script which works excellent.

sgrep:
Code:
#!/bin/sh
echo -n "Word: " 
read word
echo -n "File: "
read file
cat $file | sed ''/$word/s//`printf "\033[32m$word\033[0m"`/''
$

Code:
$ ./sgrep
Word: LINE
File: mylog
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
Word: START
File: mylog
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
$

If you copy your script e.g. in a directory which is included in your $PATH variable you can execute it with
simply issuing "sgrep" (insted of "./sgrep"), just like you intended to do with the alias stuff.
# 7  
Old 04-21-2010
Maybe try a shell function rather than a script or an alias...
Code:
$ function sgrep { sed ''/$1/s//`printf "\033[32m$1\033[0m"`/'' $2 ; }

$ sgrep START mylog
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

$ head -1 mylog | sgrep START
LINE 001 START 00:01 END 00:11

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question