Append color in shell script for output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append color in shell script for output
# 1  
Old 02-23-2012
Append color in shell script for output

Hi Experts,

I want to get my shell script output in a color for a particular word. PFB my output.

TT.QM.JTV1S1 TLORSBT2.JMR701T1.C1 REPOS
TT.QM.JTV1R1 TLORSBF2.JMR701T1.C1 NORMAL

whenever REPOS word comes then entire line should come in red color. Can you please help me out on this.
# 2  
Old 02-23-2012
Code:
awk '/REPOS/ {print "^[[31m" $0 "^[[0m"; next} {print}' your-input-file

"^[" is one non-printable character of Escape. You need to do 'Ctrl-V' follow by 'Esc' to input Escape.
# 3  
Old 02-24-2012
Or:

Code:
awk '/REPOS/ {$0=sprintf("%c[41m%s%c[0m",27,$0,27)}1'  infile


Last edited by Chubler_XL; 02-24-2012 at 12:07 AM..
# 4  
Old 02-24-2012
it is not giving the expected output in script, but it is working in single line script and O.P is coming like below
Code:
^[[41mBT.QM.REPQ01D2 SIBEMBT2.REPQ01D2.C1 BT.CL.SIBEMBT2 70.75.112.113 51422 REPOS^[[0m
^[[41mBT.QM.REPQ02D2 SIBRMBT2.REPQ02D2.C1 BT.CL.SIBEMBT2 70.75.112.114 51422 REPOS^[[0m

---------- Post updated at 05:35 AM ---------- Previous update was at 05:01 AM ----------

and can you please explain what does below do?

("%c[41m%s%c[0m",27,$0,27)}1

Last edited by vbe; 03-13-2012 at 11:13 AM.. Reason: code tags
# 5  
Old 02-24-2012
27 is the ascii value of the ESC character

The awk program pre-pends <ESC>[41m (VT100 set red rackground)
and appends <ESC>[0m (VT100 reset to normal output).

You can try it out yourself on a xterm/vt* terminal with:

Code:
awk 'BEGIN { printf("%c[41m%s%c[0m\n", 27, "Testing", 27); }'

The above will print Testing with red background.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

How to change the color inside email using shell script?

hi, i want to send an email from unix using mailx command. mailx -s "subject" "email@abc.com" < email.txt Email.txt contains some file names that are transferred successfully and some that failed. so the files that got failed to tranfer, should be displayed in red color in the mail. is it... (1 Reply)
Discussion started by: Little
1 Replies

3. Shell Programming and Scripting

How to print and append output of nawk script in commandline and as well into a file?

Hi All, I am working on nawk script, has the small function which prints the output on the screen.Am trying to print/append the same output in a file. Basically nawk script should print the output on the console/screen and as well it should write/append the same result to a file. script :... (3 Replies)
Discussion started by: Optimus81
3 Replies

4. Emergency UNIX and Linux Support

Disk space script output in color

I'm connecting to 15 servers in using ssh and storing disk space details of each server in a text file Finally , I'm emailing that text file at the particular id using mail -x . The report looks something like this. Filesystem size used avail capacity Mounted on /proc ... (30 Replies)
Discussion started by: ajaypatil_am
30 Replies

5. Shell Programming and Scripting

Color Underline and Bold letter in shell script

Hi All, This is my first port..... I am using AIX 5L, installed 10g database. On daily basis we takes rman backup. This backup status info strored in a log file. I write a script to know the status of back means I will fire this script and this script will send a mail to me. #!/bin/bash... (16 Replies)
Discussion started by: mcagaurav
16 Replies

6. Shell Programming and Scripting

putting color on output file script

do you have any simple script on how to change the color and font of a string in a script example echo "====================================" echo " sample color script" echo "====================================" echo " hello " echo " bye" on hello,... (3 Replies)
Discussion started by: lhareigh890
3 Replies

7. Shell Programming and Scripting

Shell Script to append files

I have several content (text) files in a folder called "content" I have several google ads (text) files in a folder called "google_ads" Example: /content /java websphere.txt android.txt /microsoft framework.txt /c_sharp linq.txt ... (4 Replies)
Discussion started by: rac.mike
4 Replies

8. Shell Programming and Scripting

.bashrc/PS1 command color different color to command output

I have been configuring my .bashrc PS1 to be displayed with some nice colors and in a format that I like, however there is one thing that I cannot figure out (or know if it's even possible). my PS1 line is as follows: export PS1='\\u\@\\h\:\\w\n\\$ \'This makes the command and command output... (0 Replies)
Discussion started by: jelloir
0 Replies

9. Shell Programming and Scripting

How to append the output continously from a script

Hi All, Am using the below script to produce some statistics. Currently it send the results to a log file and sends the contents of the log to a mail ID. Next time when it runs it erases the previous log and writes the latest output to the log file. I want the output to be appended to... (2 Replies)
Discussion started by: nirmal84
2 Replies

10. Shell Programming and Scripting

Specifying font type and color in a shell script

Hi, I am new to shell script. Can you please tell me, whether can we specify font type and color in a shell script? If so, how to do this? Thanks in advance. (4 Replies)
Discussion started by: Vani_Govind
4 Replies
Login or Register to Ask a Question