Results 1 to 10 of 13
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 ...
- 08-31-2007 #1Just Joined!
- Join Date
- Jul 2004
- Posts
- 24
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?
- 08-31-2007 #2
You need to escape your special charictors with a backslash
- 08-31-2007 #3Just Joined!
- Join Date
- Jul 2004
- Posts
- 24
Here's the command that's giving me the correct output:
sed -e 's#images#http://cache.somewebsite.com/images#' ajax_scaffold.css
The only problem is that the output is completely correct - it show all the replacements it made perfectly, but when I open the file, the file hasn't changed.
Also, I would like to find an argument that would allow me to do this to all files in a specified directory.
Thanks for you help.
Clem C
- 08-31-2007 #4
I would guess that sed doesn't write the changes to the file. In that case you would have to write a script to read the content, use sed to manipulate it and then write it back into the file yourself. You can then edit your script to loop through each file in a directory and preform the above on each one.
- 08-31-2007 #5Linux User
- Join Date
- Jun 2007
- Posts
- 318
You have to direct the output to a new file.
If all looks OK then you can rename it.Code:sed -e 's#images#http://cache.somewebsite.com/images#' ajax_scaffold.css > ajax_scaffold.new
EDIT: As for making changes to all files in a specific directory, you'd have to write a script using the 'find' command.
Vic
- 08-31-2007 #6
Using what vsemaska has said you could use the following bash script to automate the process:
Code:#!/bin/bash sed -e 's#images#http://cache.somewebsite.com/images#' ajax_scaffold.css > ajax_scaffold.new rm ajax_scaffold.css mv ajax_scaffold.new ajax_scaffold.css
- 08-31-2007 #7Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
If your version of sed support the -i option you don't have to use a temp file.
This line search recursively in the given directory for .css files and make the changes to all the .css files:
Replace /your/dir with the desire directory name.Code:sed -i 's#images#http://cache.somewebsite.com/images#' $(find /your/dir -name *.css)
Regards
- 08-31-2007 #8Just Joined!
- Join Date
- Jul 2004
- Posts
- 24
Great - this is exactly what I'm looking for!
how would you modify the bash script to work on a full directory?
Thanks for you help
- 08-31-2007 #9Just Joined!
- Join Date
- Jul 2004
- Posts
- 24
Franklin,
I ran this:
sed -i 's#images#http://cache.somewebsite.com/images#' $(find ./ -name *.css)
and got this:
sed: -i may not be used with stdin
looks like it's close is there an alternate to the -i arg?
- 08-31-2007 #10Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Try to give the full path with the find command.
If it dont work perhaps your version of sed don't support the -i option. Check the manpage of sed and search for the -i option.
[edit]
I apologize, *.css must be within quotes:
[/edit]Code:sed -i 's#images#http://cache.somewebsite.com/images#' $(find /your/dir -name "*.css")
RegardsLast edited by Franklin52; 08-31-2007 at 08:27 PM. Reason: Add search files within quotes


Reply With Quote