Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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?

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    You need to escape your special charictors with a backslash

  3. #3
    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

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    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.

  6. #5
    You have to direct the output to a new file.

    Code:
    sed -e 's#images#http://cache.somewebsite.com/images#' ajax_scaffold.css > ajax_scaffold.new
    If all looks OK then you can rename it.

    EDIT: As for making changes to all files in a specific directory, you'd have to write a script using the 'find' command.

    Vic

  7. #6
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    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

  8. #7
    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:

    Code:
    sed -i 's#images#http://cache.somewebsite.com/images#' $(find /your/dir -name *.css)
    Replace /your/dir with the desire directory name.

    Regards

  9. #8
    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

  10. #9
    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?

  11. #10
    -->
    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:

    Code:
    sed -i 's#images#http://cache.somewebsite.com/images#' $(find /your/dir -name "*.css")
    [/edit]

    Regards
    Last edited by Franklin52; 08-31-2007 at 09:27 PM. Reason: Add search files within quotes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •