Complex coloring in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex coloring in script
# 1  
Old 12-29-2009
Complex coloring in script

My script prints lines in which the entire line may be colored, and portions may also be colored. e.g.

Consider this to be one line:

$red some text in red $yellow abcd $end_yellow red text 1234 $blue some text $end_blue more red text $end_red

So using sed, I may based on condition 1, color the entire line red, and then based on other patterns, color portions in various colors.

I am aware of how to start coloring. However, the command to end coloring, ends *all* coloring, instead of switching off the existing color. So i cannot embed colors within other colors.

I've searched high and low but can't find anything -- any ideas.

e.g. of color switching off:

COL_NORM="$(tput setaf 9)"

or COL_NORM=$'\033[0m'

(You can refer this page for ANSI terminal codes)

Last edited by sentinel; 12-29-2009 at 12:55 PM.. Reason: corrected NORM
# 2  
Old 12-29-2009
Hi,

Try this....

Code:
print "\033[32m RED \033[0m \033[34m BLUE \033[\0m \033[32m GREEN \033[0m" ;

# 3  
Old 12-29-2009
Quote:
Originally Posted by pravin27
Hi,

Try this....

Code:
print "\033[32m RED \033[0m \033[34m BLUE \033[\0m \033[32m GREEN \033[0m" ;

Nope, i am not looking for a hard coded solution.
You are missing the part there are red portions throughout the line.

My code is like:

items = @( echo "$items" \
| sed "s/PATT1/${COLOR_RED}PATT1${COLOR_NONE}/g; \
...
/(A)/s/.*/$COLOR_YELLOW & $COLOR_NONE/;
...

)

If some patterns are found, only those patterns are printed in a color.
For other patterns, the entire line is printed in a color.

So you can have a red line with some words in blue or white etc.

However, since the code for closing a color closes all colors, so the rest of the line comes uncolored.

\033[0m - removes all color attributes. I am looking for something that will only switch off a given color, so the rest of the line can continue in the old color.

Its a bit like:
$red textaaa $green textggg $end_green textbbb textcc $end_red

so textbbb continues in red.
# 4  
Old 12-29-2009
I don't know of a way to turn off specific colors.

But I've got this to work on my dtterm (hp-ux using posix shell)

Code:
 
RED="$(tput setaf 1)"
BLU="$(tput setaf 4)"
YEL="$(tput setaf 3)"
OFF="$(tput sgr0)"
 
# given the line "Patti is great" I want to set the whole line to $RED 
#because it has the pattern "Patti" in it, but I want the word "is" to be
#yellow".
 
#variable DEF is set on the fly to the full line color.
 
#$BLU output just to establish I'm setting DEF properly
echo "Patti is great" | (DEF=${BLU};sed -ne s/Patti.*/${DEF}\&${OFF}/p)
 
#RED output
echo "Patti is great" | (DEF=${RED};sed -ne s/Patti.*/${DEF}\&${OFF}/p)
 
#FINAL output - RED 'Patti' YEL 'is' RED 'great'
echo "Patti is great" | (DEF=${RED};sed -ne s/Patti.*/${DEF}\&${OFF}/p) | sed -ne s/is/${YEL}\&${DEF}/p

You need the parenthesis to group the assignment to DEF and the sed statement into the same STDIN.

Basically, by setting the variable DEF on the fly, you can create the default color for the whole line. Then instead of turning yellow "OFF" after the word 'is' you just turn red back on with DEF.

This works for me, hopefully I didn't put a typo in. ;-)

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

Actually, further testing proved it didn't work I had DEF set in my environment, and when I unset it, I got spurious results, however the followig did work:

Code:
echo "Patti is great" | (DEF=${RED}; sed -ne s/.*Patti.*/${DEF}\&${OFF}/ -e s/is/${YEL}\&${DEF}/p)

I put the assignment to DEF and all seds into the same process using multiple -e's to separate out the sed patterns, and printing only on the last pattern of the line.
# 5  
Old 12-29-2009
Try...
Code:
(
  echo "redline etc etc blueword etc etc"
  echo "etc etc blueword etc etc"
) |\
  awk 'BEGIN {
             RED  = "\033[31m"
             BLUE = "\033[34m"
             OFF  = "\033[0m"
       }
       {
             NORM = OFF
       }
       /redline/ {
             $0 = RED $0 OFF
             NORM = RED
       }
       {
             gsub(/blueword/, BLUE "&" NORM)
             print $0
       }'

# 6  
Old 12-30-2009
Thanks a lot, and much thanks for the awk script, Ygor.

I was also thinking along these lines in my sleep ;-)

1. First color the entire line.
2. Use the line color, as the OFF color for other colors
3. Now color individual words.

Happy New Year to you all.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Coloring cli output, screws up 'alignment'

Heyas I'm trying to add color 'support' to my TUI. It works, but behaves weird. Code in question: (status.conf) R="\033" ; TUI_WORK="" TUI_DONE="" ; TUI_FAIL="" TUI_SKIP="" ; TUI_NEXT="" TUI_BACK="" ; TUI_CANC="" TUI_ON="" ; TUI_OFF="" TUI_INFO="" ; TUI_HELP="" The... (4 Replies)
Discussion started by: sea
4 Replies

2. UNIX for Dummies Questions & Answers

Coloring the pattern match

Hi All, Can we have colour the matched pattern with any color using unix command? For example I have a very length line a file and I'm applying grep on that file. grep "matched pattern" filename.txt My output is like below, ...........matched... (2 Replies)
Discussion started by: poova
2 Replies

3. Shell Programming and Scripting

Bash script - coloring reg. expressions in text

Hi all, is there anyone good at bash who will help me? I need to use syntax ${string/pattern/replacement} The problematic part where I am stuck is: #!bin/bash text="A cat is on a mat." exp="cat" newexp="SOMECODEcatSOMECODE" newtext=${${text}/${exp}/${newexp}} == > ERROR "wrong... (4 Replies)
Discussion started by: JohnnyM77
4 Replies

4. Shell Programming and Scripting

awk script (complex)

picked this up from another thread. echo 1st_file.csv; nawk -F, 'NR==FNR{a++;next} a{b++} END{for(i in b){if(b-1&&a!=b){print i";\t\t"b}else{print "NEW:"i";\t\t"b} } }' OFS=, 1st_file.csv *.csv | sort -r i need to use the above but with a slight modification.. 1.compare against 3 month... (25 Replies)
Discussion started by: slashbash
25 Replies

5. Shell Programming and Scripting

Complex Script

hey... i had a big problem with my professor i have 3 simple archives in.txt -> had all timestamps of users logon (100lines) ex. 111111 222222 333333 out.txt -> had all timestamps of users logof (100lines) ex. 111113 222225 333332 commands.txt... (9 Replies)
Discussion started by: beandj
9 Replies

6. Shell Programming and Scripting

complex find in script

How to I put my find command string into a script. It is currently to long to be entered manually at command line. for FNAME in `find /unixsxxx/interface/x.x/xxxxxx -type f \( -name '*.KSH' -o -name '*.sh' -o -name '*.sql' -o -name '*.ksh' \) -exec grep -il xxx.xxx.xxx.xxx {} \;`; do C=`grep -c... (5 Replies)
Discussion started by: TimHortons
5 Replies

7. Shell Programming and Scripting

Need complex script, anyone up for a challenge?

Default shell is /usr/bin/zsh Script will be running #!/bin/bash Need to pull information from database while using other scripts already made (not by me). Ok, so i need a script pulling certain information about a customer's router interfaces. I am using a ROUTER-DNS-NAME as variable $1 I... (3 Replies)
Discussion started by: ///NNM
3 Replies

8. UNIX for Dummies Questions & Answers

Syntax coloring for .pc files in VIM editor

Hi, i am using VIM editor through Putty. By the option of Syntax on in .vimrc file i am able to see syntax colors in .c and .cpp files but not in the files with .pc extension. How can this be done? :confused: (2 Replies)
Discussion started by: RuchK
2 Replies

9. UNIX for Dummies Questions & Answers

Coloring personal text in vim

Hi, I want to color some personal text, such as my own name in vim editor. Can anyone tell me how this is done. Thanks, Sathya (1 Reply)
Discussion started by: skkrish2
1 Replies
Login or Register to Ask a Question