LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices

Reply
 
Search this Thread
Old 08-30-2007, 07:20 PM   #1
clem_c_rock
LQ Newbie
 
Registered: Jul 2004
Posts: 5

Rep: Reputation: 0
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?
 
Old 08-30-2007, 08:23 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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)
 
Old 08-30-2007, 08:35 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,355

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
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).
 
Old 08-31-2007, 07:48 AM   #4
clem_c_rock
LQ Newbie
 
Registered: Jul 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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?
 
Old 08-31-2007, 09:15 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 08-31-2007, 12:39 PM   #6
clem_c_rock
LQ Newbie
 
Registered: Jul 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 08-31-2007, 01:48 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 08-31-2007, 01:57 PM   #8
clem_c_rock
LQ Newbie
 
Registered: Jul 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 08-31-2007, 04:06 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by clem_c_rock View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed or Awk question, looking for parsing help rwartell Linux - Software 2 05-17-2006 11:59 PM
Sed or Awk question, looking for parsing help rwartell Programming 1 05-17-2006 04:42 PM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 05:30 PM
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM
sed parsing question ncblues Linux - Newbie 5 01-03-2005 06:36 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration