LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-07-2004, 03:51 AM   #1
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Rep: Reputation: 15
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?
 
Old 09-07-2004, 05:33 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
$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.
 
Old 09-07-2004, 05:53 PM   #3
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Original Poster
Rep: Reputation: 15
sed "s/\\\<!--replace--\\\>/$TEXT/g" still returns the same error.

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

Last edited by goofyheadedpunk; 09-07-2004 at 05:57 PM.
 
Old 09-07-2004, 06:17 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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
 
Old 09-11-2004, 06:17 PM   #5
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Original Poster
Rep: Reputation: 15
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. ;)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash, using variable in sed fur Programming 3 11-12-2005 08:41 AM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 08:07 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 08:54 PM
sed not working if value is passed thru a variable containg value suchi_s Programming 7 10-29-2004 08:41 AM
sed and variable chirophil91 Programming 2 10-19-2004 04:23 AM


All times are GMT -5. The time now is 10:31 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration