Maintaining a variable(?) to prefix future text

38 views
Skip to first unread message

Peter Kaufman

unread,
Apr 30, 2020, 3:13:04 PM4/30/20
to BBEdit Talk
Folks,

I love BBEdit (v13.0.6) and hope this can do the trick!  I would rather not have to resort to sed/awk/cut/paste.

INPUT:

        April 2020

            (Individual talks will be added before the end of the month for a while.)

                [play audio] 25 Being Responsible

                [play audio] 24 In the Land of Wrong View

                [play audio] 05 Owners of Our Actions

                [play audio] 04 Wealth & Strength

                [play audio] 03 A Skillful Heart

                [play audio] 02 Stop & Think

                [play audio] 01 A Mirror for the Mind

        March 2020

            Full month zip

                [play audio] 31 Worry

                [play audio] 30 Put the Other Person’s Heart in Yours

                [play audio] 29 Protection Through Mindfulness

                [play audio] 28 Inner Worlds

                [play audio] 27 Four Mountains Moving In

                [play audio] 26 Interdependence

                [play audio] 25 Endurance & Contentment

                [play audio] 24 Alone Together

                [play audio] 23 Remembering Luang Lung

                [play audio] 12 Virtues and Values (Brazil)

        February 2020

            Full month zip

                [play audio] 16 Between Right & Wrong

                [play audio] 14 Know the Dhamma by Its Results

                [play audio] 13 The Third and a Half Noble Truth

 

OUTPUT:

Each [play audio] \d\d would be replaced with the previous YYYY-MONTH-\d\d

e.g. 

2020-April-25 Being Responsible

:               :               :

2020-April-01 A Mirror for the Mind

                2020-March-31 Worry

                                :               :               :

 

 

I know how to start this incantation:

\W*(January|February|March|April|May|June|July|August|September|October|November|December) (\d\d\d\d)\n.*$(\W*\[play audio\] (\d\d) (.*$)){1,}

 

But not how to do this.  Are there variables one can assign within BBEdit? I hate to leave BBEdit in order to apply sed/awk/grep.

 

Thanks in advance!!!

 

Peter



Neil Faiman

unread,
Apr 30, 2020, 5:04:24 PM4/30/20
to BBEdit Talk Mailing List
As far as I know, there is no one-pass solution to this problem. (I'd love to know if there is.) But there' a simple quick-and-dirty multi-pass solution. Here's one way to do it.

First, add a some character that doesn't occur anywhere else in the file to the beginning of each month line. I'm using a bullet here:

Find: ^\s*[a-z]+ \d{4}$
Replace: •&
Replace all

Gives us

       April 2020
            (Individual talks will be added before the end of the month for a while.)
                [play audio] 25 Being Responsible
                [play audio] 24 In the Land of Wrong View
                [play audio] 05 Owners of Our Actions
                [play audio] 04 Wealth & Strength
                [play audio] 03 A Skillful Heart
                [play audio] 02 Stop & Think
                [play audio] 01 A Mirror for the Mind
       March 2020
            Full month zip
                [play audio] 31 Worry
                [play audio] 30 Put the Other Person’s Heart in Yours
                ...

Next, write a pattern to replace the first (if any) [play audio] line in each month:

Find: (?s)(^•\s*([a-z]+) (\d{4})\n[^•]*?)(\[play audio\] (\d\d))
Replace: \1\3-\2-\5
Replace all

Gives us:

•        April 2020
            (Individual talks will be added before the end of the month for a while.)
                2020-April-25 Being Responsible
                [play audio] 24 In the Land of Wrong View
                [play audio] 05 Owners of Our Actions
                [play audio] 04 Wealth & Strength
                [play audio] 03 A Skillful Heart
                [play audio] 02 Stop & Think
                [play audio] 01 A Mirror for the Mind
•        March 2020
            Full month zip
                2020-March-31 Worry
                [play audio] 30 Put the Other Person’s Heart in Yours
                [play audio] 29 Protection Through Mindfulness
               ...

Now just keep hitting Cmd-Shift-= repeatedly, replacing the first unprocessed line of each month each time, until it fails. (If you have no more than a hundred [play audio] lines per month, this will take no more than a couple of minutes — i.e., much less time than it would take to find a smarter solution. If you have a thousand lines in a month, you will need some other solution.

Finally, one last search-and-replace will delete all the bullet that you inserted in step one.

Regards,

Neil Faiman

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/3081da21-3919-49a8-a16a-d634eefa3f17%40googlegroups.com.

ctfishman

unread,
May 1, 2020, 8:36:30 AM5/1/20
to BBEdit Talk
Hi Peter -

You can do this with a text filter. Save the following code in a file called something like "date_prefixer.pl", put it in your text filters folder, then go back to your file and choose Text -> Apply Text Filter - date_prefixer from the menubar.

What the script does:

1) Loops through each line in your selection (if you have one) or the entire file (if you don't).
2) If the line starts with a word, followed by a space, followed by a 4-digit number, the value of the variable "$date" is set to the number, plus a dash, plus the word, plus another dash.
3) If the line starts with "[play audio]", followed by a space, followed by a two-digit number, the text "[play audio] " is replaced with the current value of "$date", then the line is printed.
4) If neither match matches, then nothing is done and the script continues on to the next line.

I set the script to do exactly what your example says. If you want it to use the corresponding two-digit number for the month, change this line:

       $date = $2 . "-$1-";
 
to

      $date = $2 . "-$month{$1}-";
 

#!/usr/bin/perl

$month{"January"}   = "01";
$month{"February"}  = "02";
$month{"March"}     = "03";
$month{"April"}     = "04";
$month{"May"}       = "05";
$month{"June"}      = "06";
$month{"July"}      = "07";
$month{"August"}    = "08";
$month{"September"} = "09";
$month{"October"}   = "10";
$month{"November"}  = "11";
$month{"December"}  = "12";

while (<>) {
    if ( $_ =~ /^([^ ]+) ([0-9]{4})/ ) {
        $date = $2 . "-$1-";
    }
    elsif ( $_ =~ /^\[Play Audio\] ([0-9]{2}.+)/i ) {
        print $date . $1 . "\n";
    }
}

Reply all
Reply to author
Forward
0 new messages