LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed backslash question (https://www.linuxquestions.org/questions/linux-newbie-8/sed-backslash-question-758459/)

DrStrangepork 09-29-2009 09:13 AM

sed backslash question
 
I am trying to write a reporting script for my apache logs. Some of the referer URLs in my logs have backslashes in them, and my code can't figure out what to do with them, so it just errors out. I want to replace all '\' characters with '\\', and I can't get it to work. If I run this from the command line:

echo "http://music.domain.com/music.cgi?sc=mus&w=\xb4\xf3\xcd\xe6\xd0\xa6+%B9%F9%BE%B2&clz=" | sed -e 's#\\#\\\\#g'

I get this output:

http://music.domain.com/music.cgi?sc...%F9%BE%B2&clz=

But if I run it from within my script:

for i in `cat apache.log | sed -e 's#\\#\\\\#g' `; do

I get this error: "sed: -e expression #1, char 8: unterminated `s' command", and my strings are not changed. I have no idea why this isn't working...

DrStrangepork 09-29-2009 09:47 AM

I figured this out just by shooting in the dark. I know the '\' character has to be escaped, so I just tried various iterations until I got it. Here is the proper code:

for i in `cat apache.log | sed -e 's#\\\#\\\\\\\#g' `; do # first 3 '\'s, and then 7 '\'s


All times are GMT -5. The time now is 06:11 AM.