LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   General (http://www.linuxquestions.org/questions/general-10/)
-   -   Help w/ sed parsing special characters (http://www.linuxquestions.org/questions/general-10/help-w-sed-parsing-special-characters-581098/)

clem_c_rock 08-30-2007 07:20 PM

Help w/ sed parsing special characters
 
Hello,

I'm trying to change the image paths in a directory of files.

My first question is that I'm trying to use this line to test the change in one file:


sed -e 's/images/http://cache.somewebsite.com/images' ajax_scaffold.css

When I try that I get this:

sed: 1: "s/images/http://cache.r ...": bad flag in substitute command: '/'

I did a test by running this:

sed -e 's/images/duh/' ajax_scaffold.css

and the output said the changes had worked but when I went to the actual file, the changes didn't show?


Also, when I find a command that works, is it possible to specify a directory

and run this command on every file w/in the directory?

pixellany 08-30-2007 08:23 PM

In the sed "s" (substitute) command, the first character after the "s" is the delimiter. This cannot be the same as characters in the arguments unless you "escape" the latter. Also, you left out the final delimiter.

An easy fix in your example would be:
sed -e 's#images#http://cache.somewebsite.com/images#' ajax_scaffold.css

This replaces "images" with "http://cache.somewebsite.com/images" (Once per line)

syg00 08-30-2007 08:35 PM

sed streams to stdout.
If you want to update in place use "-i" - see the manpage.
(this is in addition to pixellany solution to the escape problem).

clem_c_rock 08-31-2007 07:48 AM

Great - this command doesn't give me any errors and prints the output I want perfectly, but when I actually open the file, none of the changes show?

Is there another argument I need to give it to save the changes?

pixellany 08-31-2007 09:15 AM

To change the file in place: sed -i (keeps the same filename with the new data)

To put the modified data in a new file: sed ......... >newfilename (re-direction)

Best SED tutorial: http://www.grymoire.com/Unix/Sed.html

clem_c_rock 08-31-2007 12:39 PM

I tried this:

sed -i 's#images#http://cache.somewebsite.com/images#' ajax_scaffold.css > ajax_scaffold.css

and got this:

sed: 1: "ajax_scaffold.css": command a expects \ followed by text

pixellany 08-31-2007 01:48 PM

You are trying to use the "-i" option at the same time that you redirect the output.

1. As a minimum, that is redundant
2. It may lead to errors
3. I don't think you can read from a file and then redirect to the same file

Use one or the other:

sed -i ........filename

sed .......... filename > NEWfilename

clem_c_rock 08-31-2007 01:57 PM

this is what I get w/ this:

sed -i 's/images/http:\/\/cache.reverbnation.com\/image/g' ajax_scaffold.css


sed: 1: "ajax_scaffold.css": command a expects \ followed by text

pixellany 08-31-2007 04:06 PM

Quote:

Originally Posted by clem_c_rock (Post 2877085)
this is what I get w/ this:

sed -i 's/images/http:\/\/cache.reverbnation.com\/image/g' ajax_scaffold.css


sed: 1: "ajax_scaffold.css": command a expects \ followed by text

I generated a file with the same name--including the word "images"---and ran the above command (cut and paste). It worked fine.

Assuming that there were no typos, etc., I have a hunch that there is something in your file that is being interpreted by sed or by the shell. What you might do is copy portions of the file and see if you can isolate where the "funny" is.


All times are GMT -5. The time now is 07:47 AM.