[X-Unix] Grep and backreferences with BBEdit

Nick Scalise nickscalise at mac.com
Tue Oct 12 11:44:01 PDT 2004


 On Tuesday, October 12, 2004, at 01:31PM, Brian Medley <bpm-list-osx-unix at 4321.tv> wrote:

>On Tue, Oct 12, 2004 at 01:12:25PM -0500, Nick Scalise wrote:
>
>> I do not have a strictly unix question. I am very weak at grep
>> and could use some rudimentary help.
>> 
>> In BBEdit 6.5, I am wanting to do a search and replace for all
>> occurances of <a href="name.jpg">, where 'name' is the
>> variable. I want to add a images/ to the beginning of the
>> 'name' variable.
>> 
>> So, I think I have the first part figured out, I find <a
>> href="[a-zA-Z0-9+]+.jpg"> using grep in BBEdit's find dialog,
>> this seems to produce the desired result. Please let me know if
>> there is a better way.
>> 
>> Where I am lost is the backreference part. What is the pattern
>> I would add in the replace dialog that would add 'images/'  so
>> that the original <a href='name.jpg'> would end up <a
>> href="images/name.jpg">
>
>Even though you didn't mention it, I would use perl:
>
>    $ cat test.html
>    <a href="name.jpg">
>    <h1>blah</h1>
>    <a href="name2.jpg">
>    name2.jpg
>    <a href="blue.jpg">
>    $ perl -p -i.bak -e 's#<a href="([\w\.]+)">#<a href="images/$1">#' test.html
>    $ cat test.html
>    <a href="images/name.jpg">
>    <h1>blah</h1>
>    <a href="images/name2.jpg">
>    name2.jpg
>    <a href="images/blue.jpg">
>    $ cat test.html.bak 
>    <a href="name.jpg">
>    <h1>blah</h1>
>    <a href="name2.jpg">
>    name2.jpg
>    <a href="blue.jpg">
>    $
>
>'perldoc perlrun' for more details

Thanks for the effort, but this is way over my head. But I gave it a go anyway.

I created a test.html file with:

    <a href="name.jpg">
    <h1>blah</h1>
    <a href="name2.jpg">
    name2.jpg
    <a href="blue.jpg">

in it and then ran the perl line: 

perl -p -i.bak -e 's#<a href="([\w\.]+)">#<a href="images/$1">#' test.html

It only changed the first line:

<a href="images/name.jpg">
<a href="name.jpg"> 
<h1>blah</h1> 
<a href="name2.jpg"> 
 name2.jpg 
<a href="blue.jpg"> 

Anybody with a BBedit solution?

--
Nick Scalise
nickscalise at mac.com


More information about the X-Unix mailing list