command line search and replace dilemma

D

D. Alvarado

Hello, I have a certain string that appears repeatedly in my code

style="background-image:url("/path/to/an/image");"

I would like to replace all instances with

style="background-image:url('/path/to/an/image');"

where "/path/to/an/image" is not always necessarily the same for each
instance. I learned this great command line search and replace
method:

perl -pi -e 's/expr/replacement/g' *.html

but when I try
perl -pi -e 's/background-image:url\(\"(.*?)\"\);/background-image:url(\'$1\');/g" *.html

I get the error:

-bash: syntax error near unexpected token `)'


Can anyone help? Thanks, - Dave
 
P

Paul Lalli

D. Alvarado said:
when I try

perl -pi -e 's/background-image:url\(\"(.*?)\"\);/background-image:url(\'$1\');/g" *.html

I get the error:

-bash: syntax error near unexpected token `)'

Is there some reason you use ' at the start of the program (right after
the -e), but " at the end (right after the /g)?

Paul Lalli
 
M

Michael Slass

Hello, I have a certain string that appears repeatedly in my code

style="background-image:url("/path/to/an/image");"

I would like to replace all instances with

style="background-image:url('/path/to/an/image');"

where "/path/to/an/image" is not always necessarily the same for each
instance. I learned this great command line search and replace
method:

perl -pi -e 's/expr/replacement/g' *.html

but when I try


I get the error:

-bash: syntax error near unexpected token `)'


Can anyone help? Thanks, - Dave

1) My version of perl won't accept the -i switch unless I provide an
extension for a backup file like -i.bak

2) I've replaced your .*?" with [^"]+ which saves some backtracking.

3) The answer to the heart of your question:

The shell doesn't interpret *anything* inside single quotes, including
a backslash, so you can't escape---------------------------------------------------- these ---^---^
like you're trying to. Instead, you'll need to use double quotes for
the shell, and do your escapes in there, including escaping again for perl.

Since you're going to use double quotes for the shell, you're going to
have to escape $, \, and " for the shell.

A good diagnostic for this is to replace the perl command and switches
with echo, so you can see what perl is going to get from the shell.

(pardon the overlength lines below)

perl -pi.bak -e "s/background-image:url\\(\\\"([^\"]+)\\\"\\);/background-image:url(\\'\$1\\');/g" *.html
shell converts to \ ------------------^^
shell converts to \" ---------------------^^^^
shell converts to " ----------------------------^^
shell converts to \" ---------------------------------^^^^
shell converts to \ -------------------------------------^^

And so on.

You can avoid this nastiness by placing your one-liner in a file; then
the shell doesn't get a crack at it.
 
B

Brian McCauley

D. Alvarado said:
Hello, I have a certain string that appears repeatedly in my code

style="background-image:url("/path/to/an/image");"

I would like to replace all instances with

style="background-image:url('/path/to/an/image');"

where "/path/to/an/image" is not always necessarily the same for each
instance. I learned this great command line search and replace
method:

perl -pi -e 's/expr/replacement/g' *.html

but when I try




I get the error:

-bash: syntax error near unexpected token `)'

I get

bash: syntax error near unexpected token `;'

Are you sure you cut and paste verbatin.
Can anyone help?

What you have here is a question about bash which has notihng to do with
Perl.

You know what you want to be passed into perl as the -e argument but you
need to express this string literal in bash.

The problem would be exactly the same if perl were replaced with awk,
sed, python etc.

Hint:

$ echo 'This has a " and a '"' in it"
This has a " and a ' in it
 
J

Jim Keenan

D. Alvarado said:
Hello, I have a certain string that appears repeatedly in my code

style="background-image:url("/path/to/an/image");"

I would like to replace all instances with

style="background-image:url('/path/to/an/image');"

where "/path/to/an/image" is not always necessarily the same for each
instance. I learned this great command line search and replace
method:

perl -pi -e 's/expr/replacement/g' *.html

but when I try




I get the error:

-bash: syntax error near unexpected token `)'


Can anyone help? Thanks, - Dave

Would this suffice?

perl -pi -e "s{\"([/)])}{'\$1}g" *.html

jimk
 
U

Uri Guttman

MS> 1) My version of perl won't accept the -i switch unless I provide an
MS> extension for a backup file like -i.bak

huh? all versions of perl can do -i without backups. i wager you don't
know the proper option syntax. you have to use -i by itself with no text
immediately following it. that will do inline edit and not leave a
backup.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top