LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to print part of text of a text file in bold and color using shellscript (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-print-part-of-text-of-a-text-file-in-bold-and-color-using-shellscript-890152/)

swapna sree 07-06-2011 02:14 AM

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

colucix 07-06-2011 02:37 AM

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.

swapna sree 07-06-2011 03:48 AM

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

colucix 07-06-2011 03:57 AM

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.

swapna sree 07-06-2011 04:18 AM

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

lithos 07-06-2011 04:38 AM

Hi,
you should create a file ($FILENAME ) which you will be sending in email format with HTML tags and body

swapna sree 07-06-2011 04:49 AM

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

colucix 07-06-2011 05:06 AM

Quote:

Originally Posted by swapna sree (Post 4406549)
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.

swapna sree 07-06-2011 05:33 AM

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

colucix 07-06-2011 05:41 AM

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?

swapna sree 07-06-2011 06:04 AM

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

colucix 07-06-2011 06:16 AM

Quote:

Originally Posted by swapna sree (Post 4406665)
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
:jawa:

swapna sree 07-08-2011 12:08 AM

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

swapna sree 07-08-2011 12:26 AM

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

colucix 07-08-2011 02:03 AM

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! :)


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