|
|
|
|
#1 |
|
MVP
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,617
|
Add a space using Sed (getting errors )
Hi I am trying to substitute
.jpg" to .jpg" border="2" in a load of html files. Using Code:
each \*.html sedx 's/\.jpg\"/\.jpg\" border\=\"2\"/g' The each and sedx commands normally work fine but any time i want to use a 'space' between the words i get Code:
sed: 1: "s/\.jpg\"/\.jpg\"": unterminated substitute in regular expression Here is the sedx code if it helps Code:
#!/bin/sh
tmp=tmp-file-for-$PPID
{ sed "$1" "$2" > $tmp \
&& mv $tmp "$2"
} || rm $tmp
Code:
#!/bin/bash
if [ "$2" = "" ]; then
echo "usage: ${0##*/} filetype command-to-execcute"
exit
fi
filetype=$1
shift
for file in $filetype; do
$* "$file"
done
Thanks |
|
|
|
|
|
#2 |
|
Hall of Famer
Join Date: Sep 2003
Location: Tokyo
Posts: 4,285
|
Since you say it works fine without the space, lets assume it's a shell issue. Tried a different shell?
To save you some typing in the future, you don't need to escape " and = characters, just / and ' Here, running 's/\.jpg"/\.jpg" border="2"/g' works as you want it to. If this is a global change, just add img {border-width: 2px; border-color: red; border-style: solid; } to the style sheet and you're all done! Last edited by acme.mail.order; 09-20-2004 at 01:33 AM. |
|
|
|
|
|
#3 |
|
MVP
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,617
|
Thanks , I guess I was being thick and could have just used the .css file.
But I think I have been trying to figure this problem for a while and got caught up in a loop. I Still need to sort this out though because there I times when I need to change caption text on a whole load of web pages, and this is the best/quickest way, so long as I only do single words. But I need to be able to do it with more text. I did try your script and change the shell, but no change. do you have a list of the ones that are installed with Osx thanks |
|
|
|
|
|
#4 |
|
All Star
Join Date: Feb 2003
Location: Chico, CA
Posts: 675
|
Just a thought...have you tried escaping the space? Since the raw command works for acme, maybe your quoting is getting munged a bit being passed between variables and commands.
each \*.html sedx 's/\.jpg\"/\.jpg\"\ border\=\"2\"/g' |
|
|
|
|
|
#5 | |||||||||||||||||||
|
MVP
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,617
|
Yes tried that, but no luck.
But I think your right
I just tried the sedx part on it own and it worked sedx 's/\.jpg"/\.jpg" border="2"/g' index_4.html so Now all I need to do is work out why the each is trashing the string? |
|||||||||||||||||||
|
|
|
|
|
#6 | |||||||||||||||||||||||
|
Prospect
Join Date: Sep 2004
Posts: 5
|
Bash internal commands consider the space character a line separator. It'll screw it up even if the space is escaped in a for each loop. However, you can do something like: Code:
newifs=$IFS; IFS="<tab><newline>"; each \*.html sedx 's/\.jpg\"/\.jpg\" border\=\"2\"/g'; IFS=$newifs Good luck! PS: That "<tab><newline>" above means quote-key tab-key return-key quote-key. If you type it in literally, you'll have some really screwy line separators .
Last edited by squinty; 09-20-2004 at 10:32 PM. |
|||||||||||||||||||||||
|
|
|
|
|
#7 | |||||||||||||||||||
|
MVP
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,617
|
Squinty, you are Fantastic, that worked as I needed when put into the each
Command. Thanks I am not going to even pretend I understood why that worked. But I did have a look in the Man Page for bash, and found
I think I will need to read it a couple of hundred times to understand it. I guess what you got me to change was the defualt <space><tab><new-line> to <tab><new-line> But I need to get a handle on the expansion stuff, thanks again, and Boy have I got a long way to go in my learning curve. |
|||||||||||||||||||
|
|
|
|
|
#8 | |||||||||||||||||||||||
|
Prospect
Join Date: Sep 2004
Posts: 5
|
That's right. You'll find IFS is really useful if you want to have bash loop over filenames with spaces in them or anything like that. |
|||||||||||||||||||||||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|