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?
# 8  
Old 04-21-2010
That's awesome! totally worked. .. Hmmm.. functions eh that's new territory for me. I'll have to start poking around with what those are.

You have any links that you find particularly good on the subject? I'll do my own searching of course just curious if you have any recommends.
# 9  
Old 04-21-2010
A shell function works like a function in any programming language. Remember, a shell is never only a commando interpreter but always a programming language as well.

If you have no experience with programming picture it like this: you create a set of commands and put a name to this collection. When you now call the name you have selected the whole collection of these commands is being replayed.

So far there is no difference between a script (=program) and a function at all. The difference is that a script always uses its own environment (it is a program in its own right), whereas a function is executed within the environment of another program. In the shell you can even "load" functions into memory and have them being executed within your shell without having to load them from disk, like a script. In this case the program within which it si executed is the command shell you are using.

I hope this helps.

bakunin
# 10  
Old 04-21-2010
Any idea if this function could work with wildcards at all?

I've tried with various wildcards so far and that doesn't seem to work.

So I could do something like... *tatus or 50**

---------- Post updated at 12:11 PM ---------- Previous update was at 11:48 AM ----------

Ok tell me if this is an accurate understanding. I was trying to grasp the difference between the alias and the function. Simply looking at them it seems the same.

But what I think is going on is that a function is it's own isolated execution. Meaning if I do.

Code:
alias word=somecommand

Then in at any time I put in 'word', all that is really happening is that 'word' is removed and the other words put in the same place.

Where as a function is actually executing its own command right there which is isolated from the ones around it? Or am I completely off here?

---------- Post updated at 03:13 PM ---------- Previous update was at 12:11 PM ----------

Well I have made a little progress here. I adjusted my function a little bit in an effort to make it work with multiple words instead of just one. I have gotten this to work the problem however is that its always a fixed amount. If I use this function then I can change the color of two words but its MUST be two words or it will not work.

Code:
function sgrep { sed -e ''/$1/s//`echo -e "\033[32m"$1"\033[0m"`/'' -e ''/$2/s//`echo -e "\033[32m"$2"\033[0m"`/''  $3 ; }

So i'm trying still to figure out how to simply get this to work with whatever # of words or strings I enter.

I changed it from printf to echo just to play around but either works really.

---------- Post updated at 04:40 PM ---------- Previous update was at 03:13 PM ----------

Perhaps I could use some kind of control variable and make SED loop X number of times based on that variable... So... I could do something like.

Code:
cat report.log | sgrep 4 report fish tree

Then I use the first argument as my loop #. Then sed goes through once then subtracts one from the loop number and goes through again until the number reaches zero... Sounds complicated for a task such as this.. But for me its more about learning how all this stuff works vs how actually useful these commands wind up being in the real world.

---------- Post updated at 05:10 PM ---------- Previous update was at 04:40 PM ----------

Ok so i've gotten this far.. It works but only with 1 variable still...

Code:
function sgrep { for FN in $* ; do sed ''/$FN/s//`printf "\033[32m$FN\033[0m"`/'' ; done }



---------- Post updated at 05:44 PM ---------- Previous update was at 05:10 PM ----------

Seems to be an issue with the variables.. Clearly the command is working so its getting the first variable. But ones don't seem to be coming through.

So when I do sgrep Front log error

It finds front and does what it should but there is no loop happening and its not picking up the log or error attributes...

ITS A BREAK IN THE LINE I'M LOST!!!!

Last edited by Scott; 04-21-2010 at 07:37 PM.. Reason: Please use code tags
# 11  
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!
$

# 12  
Old 04-21-2010
Code:
sed "s/START/"$'\e[32m'"&"$'\e[m'"/"

!!
# 13  
Old 04-22-2010
Thanks i'm glad you were able to get the sed command to work correctly without actually using it as a function.

I still think that my loop method seems like it should work but its not. I just need to figure out why its only taking the first argument and using it as a variable. If it would just take all 3 like I WANT IT TOO! then it would work fine and it would work for any number of works I used anytime.

The script that pseudocoder wrote i'm sure will work. Problem is that the way our systems work I'm not working on my own box. I'm working on customers boxes so I don't like having to put anything on the customers box. So almost everything I do has to be something which can be pasted into a command line. Having to FTP a file to a customers box is a no no unless its an unusual situation.

So I can't really use the script except on my own boxes at my desk.
# 14  
Old 04-22-2010
Try...
Code:
function sgrep { r="$1" ; shift ; awk '{gsub(r,"\033[32m&\033[0m")};1' r="$r" $* ; }

Usage...
Code:
sgrep "START|END" file1 file2 file3

...or...
Code:
head -1 file1 file2 file3 | sgrep "START|END"

 
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