LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2011, 02:14 AM   #1
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Rep: Reputation: 0
how to print part of text of a text file in bold and color using shellscript


Hi all,

I need a help...

i have a procedure say X
output of X is :
Fund name: Mutual
NAV is: 1234

So i appended this output into a file using shellscript
But now my requirement is changed
the part of output like Fund name and 1234 should be in bold..
How can i do this using shell script

Let me know,if anyone knows

thanks & regards
Swapna
 
Old 07-06-2011, 02:37 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi and welcome to LinuxQuestions!
Code:
#!/bin/bash
echo -e "\033[1mFund name\033[0m: Mutual"
echo -e "NAV is: \033[1m1234\033[0m"
Here the -e option enables interpretation of backslash escapes. The ANSI escape sequences \033[1m and \033[0m switch the bold text on and off respectively. You can also use the sequences to colorize the text. Please see http://www.tldp.org/LDP/abs/html/colorizing.html for more details.
 
Old 07-06-2011, 03:48 AM   #3
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks for the quick reply colucix

if i use like \033[1m and all the other stuff its printing command prompt of Putty.
But the effect is not implied in a file

I need to send a file to User in which the part of text sholud be in bold....
 
Old 07-06-2011, 03:57 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You cannot embed color information in a plain text file. It is the application that reads the file that colorizes the output (like echo in the example above), but there is no bold or colorized text inside the file itself.
 
Old 07-06-2011, 04:18 AM   #5
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Original Poster
Rep: Reputation: 0
In shell script,
filename may not be only plain text,I tested with .rtf and .html and .c and also with .s
its not displayin g in bold ,instead the file is having \033[1m Test as the data
======
echo "CALLING THE PROCEDURE"
setenv DBSQL "exec PROCEDURENAME"
mail -s "Message Title" $EMAILTO < $FILENAME
=====
The above procedure displays
FundName : Mutual
NAV is 1234

====
So i appended this output into $FILENAME and i need to mail this content of a file to user and the content is displayed in BODY of mail
So,i need a part of text in BOLD
 
Old 07-06-2011, 04:38 AM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,
you should create a file ($FILENAME ) which you will be sending in email format with HTML tags and body
 
Old 07-06-2011, 04:49 AM   #7
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Original Poster
Rep: Reputation: 0
How can we embed the dynamic procedure ouput into $FILENAME if we specify the HTML tags
the output sholud embed into the Body of MAIL with the BOLD data
 
Old 07-06-2011, 05:06 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by swapna sree View Post
So i appended this output into $FILENAME and i need to mail this content of a file to user and the content is displayed in BODY of mail
So,i need a part of text in BOLD
This is another story! You need the mail body to be colorized, not the text file, right? First the mail need to be in HTML format, since a plain text mail body cannot manage color information. Then you have to transform the text file into HTML code.

Here we go:
Code:
#
#  Do TXT to HTML conversion (uses AWK)
#
awk '
  BEGIN { print "<html>"
          print "<head>"
          print "</head>"
          print "<body bgcolor=\"#ffffff\" text=\"#000000\">"
          print "<pre>"
  }

  { 
    gsub(/Fund name/,"<b>Fund name</b>")
    gsub(/1234/,"<b>1234</b>")
    print $0
  }

  END { print "</pre>"
        print "</body>"
        print "</html>"
  }

' $FILENAME > $FILENAME.html

#
#  Send mail (uses SENDMAIL)
#
(
 echo "From: Me <me@somewhere>"
 echo "To: You <you@somewhere>"
 echo "Cc: Someone Else <someoneo@somewhere>"
 echo "Subject: Message Title"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 echo
 echo "<pre>Any text here</pre>"
 echo
 cat $FILENAME.html
 echo
) | /usr/sbin/sendmail "$EMAILTO"
If you want to colorize the text, you have to use the <font color> tag, e.g.:
Code:
gsub(/Fund name/,"<font color=\"#ff0000\">Fund name</font>")
specifying the color in HTML format (the example above transform the text in red). Hope this helps.
 
Old 07-06-2011, 05:33 AM   #9
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Original Poster
Rep: Reputation: 0
Hi colucix

When i tested your code,
The code which you gave its giving error like Unmatched '
after awk command
may i know what is the reason
 
Old 07-06-2011, 05:41 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The only single quotes in my code are:
Code:
awk '
...
...
...
' $FILENAME > $FILENAME.html
that is those ones that embed the awk code. Maybe did you add some other single quote inside the awk code?
 
Old 07-06-2011, 06:04 AM   #11
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Original Poster
Rep: Reputation: 0
HI

Even now its getting the same problem Unmatched '

It works in csh shell script right?
can you plz test itr once ...
I need to send this report to my PD before i leave from office
 
Old 07-06-2011, 06:16 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by swapna sree View Post
HI

Even now its getting the same problem Unmatched '

It works in csh shell script right?
can you plz test itr once ...
I need to send this report to my PD before i leave from office
Nope, it's a bash script! Indeed using a C-shell it returns the unmatched ' error! Use the sha-bang at the beginning of your script to avoid problems:
Code:
#!/bin/bash

Last edited by colucix; 07-06-2011 at 06:19 AM.
 
Old 07-08-2011, 12:08 AM   #13
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Original Poster
Rep: Reputation: 0
Hiii,


I need to run my change only in csh

Because i cant change the code and i dont have access to change the existing code,If i change the csh to bash entire shell script has to change

Can anyone provide me any alternate idea/solution ,to get the part of data in bold in csh
 
Old 07-08-2011, 12:26 AM   #14
swapna sree
LQ Newbie
 
Registered: Jul 2011
Location: Hyderabad
Posts: 20

Original Poster
Rep: Reputation: 0
Hi colucix

if i tried your example using bash script itself

gsub(/Fund name/,"<b>Fund name</b>")
gsub(/1234/,"<b>1234</b>")

Its prompting to enter the data after the execution of above 2 statements
If i entered Fund name and 1234 or anything its not comming out of that loop

and i need to press CTRL+C to get exit from the program

Can you plz tell me how to avoid this problem
 
Old 07-08-2011, 02:03 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The gsub statements are part of the awk script, they should not ask for user input, unless you don't pass any argument (that is the file to process) to awk.

Anyway, the suggested code should work even in csh with slight modification. The awk part is the same. /bin/csh does not accept the single quote at the end of the line
Code:
awk '
Maybe you need to put a backslash at the end of each line to tell the shell that the command continues on the next line. I cannot try on a C-shell right now, but I will do within today. So stay tuned, coming back soon!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
print text color using perl john83reuben Programming 4 12-04-2009 12:11 AM
How change text color using linux in text mode only runlevel 3? Xavius Linux - General 7 05-07-2009 02:19 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
how to print text in color by ncurses without opening a ncurses window Greatrebel Programming 0 12-20-2006 09:15 AM
Konqueror (webbrowser part) only displays text in BOLD jeroenvrp Linux - Software 0 07-24-2004 09:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration