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 /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:
Anyone see what I'm doing wrong? |
$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. |
sed "s/\\\<!--replace--\\\>/$TEXT/g" still returns the same error.
Just as a note, $TEXT contains HTML formatted text. |
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 ;) |
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. |