Site TIPS PAGE🔎   UP ONE LEVEL
 OJB's Web Site. V 2.1.tip tips mac owen2 
Tip

Up to OJB's Mac Tips List

Use GREP in BBEdit

GREP is a "language" for searching in text files. It allows you to do much more than a standard search in a word processor. The acronym "GREP" stands for "global regular expression parser" and originated in the Unix world. If you use Mac OS X you can go to the terminal and type man grep to get the GREP manual. On this page I am discussing the implementation of GREP in the Mac applciation BBEdit, which supports search and search and replace using GREP expressions. BBEdit is one of the most useful programs ever! I use it a lot, and GREP is one of the reasons why.

To do a GREP search and replace you need to specify what to look for and what to replace it with. Listed here are a few of my favourites which I have created myself. I'm sure there are more complex examples created by more experienced GREP users on the Internet. To search, open the file to be searched in BBEdit (or specify the folder to look for multiple files) and enter an expression to find and another to replace it with. In these examples "ø" means space.

Purpose: Find digit between /s, eg /1/, add a zero between first / and digit
Example: Expand 2nd part of date (month) to have leading zero
Find: /([123456789])/
Replace: /0\1/
Info: The / is just the slash to search for. The () enclose what to remember in the find to be replaced in the replace. In this example find a digit and put it back in the replace string. The [] specify a series of digits that can occur ([1-9] would also work). The "0" is just the literal 0 to add. The \1 means take what was found inside the first () and put it back here.

Purpose: Swap month and day part of date string.
Example: Change 01/20/2000 to 20/01/2000
Find: (..)\/(..)\/
Replace: \2\/\1\/
Info: The .. looks for 2 digits. The () encloses what to remember. The \/ just means slash. The \2 says put the 2nd thing remembered here and the \1 means put the first thing rememebred here.
Problems: Must be 2 digits in day and month. Any 2 digits between slashes will be treated as dates.

Purpose: Swap month and day part of date string, where the date is the first item on line.
Example: Change 01/20/2000 to 20/01/2000
Find: \r(.+)\/(.+)\/
Replace: \r\2\/\1\/
Info: The \r means return. The .+ looks for a sequence of digits. The () encloses what to remember. The \/ just means slash. The \2 says put the 2nd thing remembered here and the \1 means put the first thing remembered here.
Problems: Must be first item on the line. Any digits between slashes will be treated as dates. End-of-line must be return.

Purpose: Find any sequence of tabs and replace with one tab
Example: Tidy text formatted for default tabs
Find: \t\t+
Replace: \t
Info: The \t looks for a single tab. The \t+ means any number of tabs.

Purpose: Find commas in numbers and remove them (add a space at front to re-align the number)
Example: Remove commas in numbers in space delimited files.
Find: (#*),
Replace: \1
Info: The #* looks for any sequence of digits. The () says remember this sequence. The \1 replaces the sequemce remembered. There is one space in front of the first line and 2 in front of the 2nd.
Problems: Only removes first comma. Repeat to remove others. Number can't be first on line and must have a space in front.



My latest blog post: Manufactured Outrage: We might be beginning to win some battles, but the war is far from over. (posted 2024-12-17). My latest podcast: OJB's Podcast 2024-12-04 Avoid Microsoft.
 ©2024 by OJBServerMS Free ZoneMac Made
T: 11. H: 216,388,650
Features: RSS Feeds Feedback LogMod: 07 Jun 2024