[X-Unix] Grep and backreferences with BBEdit

Brian Medley bpm-list-osx-unix at 4321.tv
Tue Oct 12 11:31:36 PDT 2004


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

-- 
~`^`'~=-._.-=~'`^`'~=-._.-=~'^'~=-., \|/  (___)  \|/ _,.-=~'`^`
                                      @~./'O o`\.~@
      "Knowledge is Power"           /__( \___/ )__\  *PPPFFBT!*
         -- Francis Bacon               `\__`U_/'
 _.-=~'``'~=-._.-=~'``'~=-._.-=~'`^`'~= <____|'  ^^`'~=-.,__,.-=
~'^`'~=-._.-=~'`^`'~=-._.-=~'^'~=-.,__,.-==--^'~=-.,__,.-=~'`^`


More information about the X-Unix mailing list