LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Expanding shell variable in sed (https://www.linuxquestions.org/questions/programming-9/expanding-shell-variable-in-sed-227588/)

goofyheadedpunk 09-07-2004 02:51 AM

Expanding shell variable in sed
 
Currently I am attempting to write a bash script that automagically formats ascii IM logs and appends it to a larger HTML file for my website. The current page can be seen here (warning: may contain material, all in text of course, objectionable to those who don't realize it's so absurd as to be harmless)

Here is my code:
Code:

#!/bin/bash

#adds newline to end of backup file
echo -e '\n' >> /goofyheadedpunk/logfile.backup
#appends file to be processed to backup
cat /home/blt/logfile >> /goofyheadedpunk/logfile.backup

#rather messy
#the sed takes an unformated ascii, and formats it to HTML
#this is then output to the variable TEXT
TEXT=$(sed -e 's/^[^:]*:/<font color="#a82f2f"><b>&<\/b><\/font>/g' /home/blt/logfile -e 's/<font color="#a82f2f"><b>goofyheadedpunk:<\/b><\/font>/<font color="#16569e"><b>goofyheadedpunk:<\/b><\/font>/g' -e 's/<font color="#a82f2f"><b> goofyheadedpunk:<\/b><\/font>/<font color="#16569e"><b>goofyheadedpunk:<\/b><\/font>/g'  -e 's/$/<br>/' #-e 's/^<br>$/<\/p><p>/')

#broken, needs help
#in using double quotes to expand TEXT recieve error message
# "sed: -e expression #1, char 63: Unknown option to `s'"
sed "s/\<!--replace--\>/$TEXT/g" /goofyheadedpunk/misc/socialization.html

#upload socialization.html to server (after recent addition)
/bin/ftp -v << !
open mpn.ath.cx
cd misc
put /goofyheadedpunk/misc/socialization.html socialization.html
bye
!

/home/blt/logfile is the file I drop unformated ascii text into while I'm chatting. What happens is that my friends and I will have a particularly absurd exchange and I'll copy directly from my gaim window into the aforementioned file. Each new entry is separated from the last by a newline.

/goofyheadedpunk/logfile.backup is, well, a backup of /home/blt/logfile that is made before each processing of /home/blt/logfile. /goofyheadedpunk/misc/socialization.html is the source on my local machine to the above mentioned page.

I apologize for the rather tortuous sed bit of my script. If you don't want to sift through it, all that does is format the ascii of /home/blt/logfile into HTML for the above linked webpage and then assigns it to TEXT. TEXT exists so that I could replace a comment in /goofyheadedpunk/misc/socialization.html with new, formated, input from /home/blt/logfile.

The difficulty I run into is that $TEXT won't expand without double quotes ( at least I think it is now ) but when I use double quotes I get this error
Quote:

sed: -e expression #1, char 63: Unknown option to `s'
I've played with the quotes, trying combinations of single quotes and double quotes, but I can't get anything to work.

Anyone see what I'm doing wrong?

druuna 09-07-2004 04:33 PM

$TEXT is not the problem, the sed statement is:

sed "s/\<!--replace--\>/$TEXT/g"

Should be:

sed "s/\\\<!--replace--\\\>/$TEXT/g"

Hope this gets you going again.

goofyheadedpunk 09-07-2004 04:53 PM

sed "s/\\\<!--replace--\\\>/$TEXT/g" still returns the same error.

Just as a note, $TEXT contains HTML formatted text.

druuna 09-07-2004 05:17 PM

All of the special characters need to be escaped when you give them to TEXT.

#!/bin/bash

TEXT_01="<font color="#a82f2f"><b>foobar</b></font>"
TEXT_02="<font color=\"\#a82f2f\"><b>foobar<\/b><\/font>"

echo ""
echo "Non escaped"
sed "s/\\\<!--replace--\\\>/${TEXT_01}/g" testfile
echo ""
echo "Escaped"
sed "s/\\\<!--replace--\\\>/${TEXT_02}/g" testfile
echo ""

# tesfile holds this:
#
#\<!--replace--\>
#
#\<!--replace--\>


Output:

Non escaped
sed: -e expression #1, char 53: Unknown option to `s'

Escaped

<font color="#a82f2f"><b>foobar</b></font>

<font color="#a82f2f"><b>foobar</b></font>


I think that your TEXT=$(sed -e...... needs work and you do need to use the previous 'enhancement' I suggested ;)

goofyheadedpunk 09-11-2004 05:17 PM

Thanks for all your help, but I decided that I really am not fond of using bash scripts to do large amounts of text manipulations.

So I spent a couple of days learning perl and this popped out. The array assignment when I make my HTML throws a warning, but I'm pretty sure that can be fixed by using "join".

I just haven't done it yet. ;)


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