BBEdit’s grep replacement feature is not as robust as most; most people get smart and learn perl or another language with a deeper definition of features, but I don’t have that time today. What I need is to 1) replace a series of text that is randomly identified and 2) number it sequentially.
Now in normal grep, this would be a bit easier than expected by applying a Regular Expression Counter to your replacement:
e%0>
Would sequentially add a number greater to zero. Brilliant! Unsupported in BBEdit!
Now, again, you could code in a different language and accomplish this much easier if you have that experience, but for me, the trick was to step around BBEdit’s limitation with line number.
The Sequential Numbering Trick
- Under Text, Select the Add/Remove Line Numbers…
- In my case, I’ve chosen to start at 0 and then add a space (the space is just to give me more grep characters to call). I’ve removed Justified so it doesn’t add any space before my number, skewing my search to come. When you’re ready, go ahead and Number:
- Now we’ll start a find/replace of our information under Search > Find…
- In this case, I’m going to edit:
So that the line numbers are remove from the left, and the text ‘60′ is replaced by them using a specific grep pattern:0 <li>Brady</li> 1 <li>Brady</li> 2 <li>Brady</li> 3 <li>Brady</li>
- The final result is exactly what I needed:
<li>Brady 0</li> <li>Brady 1</li> <li>Brady 2</li> <li>Brady 3</li>
What does that code mean?
As I noted before, Grep in BBEdit is limited, although there’s power to be had (and I recommend Cari D. Burstein’s overview). While I can’t call much in the replacement command, I can call data from the find portion and pass it along to the replacement area.
In the case of the find:
(\d*) <li>Brady</li>
I’ve used parenthesis to tell BBEdit these are ‘groups’ to be recognized later on; these groups are counted as group 1, then group 2, then group 3, and so on. Inside these parenthesis I use the \d command to mean ‘a numerical digit’ and the asterisk to mean ‘as many digits as I’d like’. I’ve now set myself up to search for the numerical line number as a group, and reuse it in my replace.
Now I’m ready to replace my content:
<li>Brady \1</li>
The \1 means ‘replace this area with content from the first group you called in the search’. The end result is that the search number is moved here, and since I didn’t add any command for the group to be repeated at the beginning of the line, the first line numbering is removed.
Using this in an advanced scenario
One of our entry, and rushed, business ventures under my scope is for my Boss’ beautiful new line of Jewelry, Amber Marie Bently Jewelry. She’s recently designed a new series of pieces, and I’d like those designs to be seated at the front of the web list. Since I’ve used a Javascript slider, I’ll have to put all the list content at the top, and then dynamically change the sequential order of each full size image and each Thumbnail… annoying by hand, less than a second with Grep replacement.
- Each line is out of order, and for example, is a long series of code:
0 <li><a href="#"><img id="thumb57" src="thumbnails/test1.jpg" width="60" height="42" alt="thumbnail 60"></a></li> - I’m going to search for each line, making sure to call grep in areas where information is different so no line is left behind:
(\d*) <li><a href="#"><img id="thumb(\d*)" src="(.*)" width="60" height="42" alt="thumbnail (\d*)"></a></li> - Now, I’m going to call the first group into the thumb ID and into the alt thumbnail text, leaving the third group in the same spot so my URL isn’t altered:
<li><a href="#"><img id="thumb\1" src="\3" width="60" height="42" alt="thumbnail \1"></a></li>
…and that’s it. 98 thumbnails and 98 full size images magically ordered on the fly in real time.
Similar Posts
4 Reader Comments:
-
June 10th, 2008 9:36 pm
Douglas Hahn
Amber Marie’s site looks beautiful, and this is a great little BBEdit tip!
-
June 11th, 2008 7:32 am
Brady J. Frey
I figured someone would need it someday too, I can’t find a single technique on google for the above; save using applescript, perl, etc.
-
June 11th, 2008 4:51 pm
Joe
I can’t read your white on black text!!!!!!!!!!!!!!!!!!!!!
-
June 12th, 2008 4:35 pm
Brady J. Frey
I have a strong love for reversed out text, but I can understand your pain… maybe it’s time for a redesign? Well, let me try to swing the color scheme a bit and I’ll get back to you;)