Go Back   The macosxhints Forums > OS X Help Requests > UNIX - Newcomers



Reply
 
Thread Tools Rate Thread Display Modes
Old 09-19-2004, 07:06 AM   #1
mark hunte
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
And here is the each code
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
OSX 10.2.8

Thanks
mark hunte is offline   Reply With Quote
Old 09-19-2004, 10:06 AM   #2
acme.mail.order
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.
acme.mail.order is offline   Reply With Quote
Old 09-19-2004, 04:03 PM   #3
mark hunte
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
mark hunte is offline   Reply With Quote
Old 09-19-2004, 04:50 PM   #4
jbc
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'
jbc is offline   Reply With Quote
Old 09-19-2004, 06:06 PM   #5
mark hunte
MVP
 
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,617
Yes tried that, but no luck.

But I think your right
Quote:
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.

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?
mark hunte is offline   Reply With Quote
Old 09-20-2004, 10:27 PM   #6
squinty
Prospect
 
Join Date: Sep 2004
Posts: 5
Quote:
Originally Posted by mark hunte
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?

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
or you could add the IFS redefinition to your 'each' code. In case you didn't guess, the variable IFS is a built-in that defines line separators for your shell.

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.
squinty is offline   Reply With Quote
Old 09-21-2004, 03:33 PM   #7
mark hunte
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

Quote:
IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read
builtin command. The default value is ``<space><tab><new-
line>''.

Word Splitting
The shell scans the results of parameter expansion, command substitu-
tion, and arithmetic expansion that did not occur within double quotes
for word splitting.

The shell treats each character of IFS as a delimiter, and splits the
results of the other expansions into words on these characters. If IFS
is unset, or its value is exactly <space><tab><newline>, the default,
then any sequence of IFS characters serves to delimit words. If IFS
has a value other than the default, then sequences of the whitespace
characters space and tab are ignored at the beginning and end of the
word, as long as the whitespace character is in the value of IFS (an
IFS whitespace character). Any character in IFS that is not IFS
whitespace, along with any adjacent IFS whitespace characters, delimits
a field. A sequence of IFS whitespace characters is also treated as a
delimiter. If the value of IFS is null, no word splitting occurs.

Explicit null arguments ("" or '') are retained. Unquoted implicit
null arguments, resulting from the expansion of parameters that have no
values, are removed. If a parameter with no value is expanded within
double quotes, a null argument results and is retained.

Note that if no expansion occurs, no splitting is performed.

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.
mark hunte is offline   Reply With Quote
Old 09-21-2004, 04:59 PM   #8
squinty
Prospect
 
Join Date: Sep 2004
Posts: 5
Quote:
Originally Posted by mark hunte
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>

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.
squinty is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:11 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Site design © Mac Publishing LLC; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of Mac Publishing LLC.