Need to increment a counter for chapter count

141 views
Skip to first unread message

David G Wagner

unread,
Jan 5, 2022, 3:19:08 PM1/5/22
to BBEdit Talk
I have the following data:

#Start#
1 In the beginning, God created the heavens and
Xxxxxxccx
#EndOfGen#
#Start#
1 Thus the heavens and the earth were finished,
Xxxxxxxx
#EndOfGen#
#Start#
1 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
Xxxxxxx
#EndOfGen#
….

In this case, I have 50 sets of start and end groups. The output I want is:

#Start#
1 In the beginning, God created the heavens and
Xxxxxxccx
#EndOfGen#
#Start#
2 Thus the heavens and the earth were finished,
Xxxxxxxx
#EndOfGen#
#Start#
3 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
Xxxxxxx
#EndOfGen#


What I am doing is placing the chapter count as the first number after the start.

I have data in one file and in one window. I could write a Perl script, but was wanting to handle within BBEdit via filter or ?

I see only one variable, that being the chapter count.

Thoughts would be appreciated.

Thanks…

Wags ;)
WagsWorld
Hebrews 4:15
Ph(primary) : 408-914-1341
Ph(secondary): 408-761-7391

Christopher Waterman

unread,
Jan 5, 2022, 9:01:16 PM1/5/22
to bbe...@googlegroups.com
Hello David,

Here is a Text Filter that might work.

Copy and paste the following into a file. ( Starting with the ‘#' and ending with the last ‘}’ )


#!/usr/bin/env awk -f

BEGIN {
RS = "#Start#"
ORS = ""
FS = "\n"
OFS = "\n"
}

sub( /[0-9]+/, NR , $2 )
print "#Start#" $0
}


Name it something like ’Set Chapter Num.awk’ and save it to '~/Library/Application Support/BBEdit/Text Filters'.

Then open the file you want to edit and run the script from the ’Text > Apply Text Filter’.

Hope it works.
— Chris(topher)?



--
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/5ab44bf1-95cd-4ca8-aa4c-d798f9d12deb%40Spark.

David G Wagner

unread,
Jan 5, 2022, 10:04:23 PM1/5/22
to bbe...@googlegroups.com
Thank, Chris but did not do as desired.

Here is the output received(cutdown from my extended data test):

#Start##Start#start#Start#
3 In the beginning, God created the heavens and the 
#Start#EndOfGen#Start#
#Start#Start#Start#
7 Thus the heavens and the earth were finished, and #Start#EndOfGen#Start#
#Start#Start#Start#
11 Now the serpent was more crafty than any other #Start#EndOfGen#Start#

Desired out:

#Start#
1 In the beginning, God created the heavens and the 
#EndOfGen#
#Start#
2 Thus the heavens and the earth were finished, and #EndOfGen#

#Start#
3 Now the serpent was more crafty than any other
#EndOfGen#

What each start and end is a chapter. In the data. You Have the start and then 1. What I want to do is replace the 1 with the current chapter number. First hit is Chapter 1. The fifth would be Chapter 5, etc.

Thoughts??? 

Wags ;)
WagsWorld
Hebrews 4:15
Ph(primary) : 408-914-1341
Ph(secondary): 408-761-7391

Christopher Waterman

unread,
Jan 5, 2022, 10:52:43 PM1/5/22
to bbe...@googlegroups.com
Sorry David,

I did make a mistake but the output you're getting still confuses me.
All my chapter numbers were off by one, but that was it. 

Anyway here is the fixed script. ( I think 🤞)

I provided an example of my input and output, also a screenshot.
Be sure to copy the script exactly.

#!/usr/bin/env awk -f

BEGIN {
RS = "#Start#"
ORS = ""
FS = "\n"
OFS = "\n"
}


sub( /[0-9]+/, NR-1 , $2 )
}

NR != 1 {
print "#Start#" $0
}

Input:
#Start#
1 In the beginning, God created the heavens and
Xxxxxxccx
#EndOfGen#
#Start#
1 Thus the heavens and the earth were finished,
Xxxxxxxx
#EndOfGen#
#Start#
1 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
Xxxxxxx
#EndOfGen#

Output:
#Start#
1 In the beginning, God created the heavens and
Xxxxxxccx
#EndOfGen#
#Start#
2 Thus the heavens and the earth were finished,
Xxxxxxxx
#EndOfGen#
#Start#
3 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
Xxxxxxx
#EndOfGen#

Here is a screenshot of the results.


— Chris(topher)?



--
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.

Christopher Stone

unread,
Jan 6, 2022, 7:43:35 AM1/6/22
to BBEdit-Talk
On Jan 05, 2022, at 14:15, David G Wagner <wagsw...@gmail.com> wrote:
I have data in one file and in one window. I could write a Perl script, but was wanting to handle within BBEdit via filter or ?


Hey David,

A Perl script can be employed as a text filter, and that's probably as simple as anything.

~/Library/Application Support/BBEdit/Text Filters/<Your Filter>



#!/usr/bin/env perl -sw
# -----------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/01/06 06:35
# dMod: 2022/01/06 06:35
# Task: Number Lines Based On A Condition.
# Tags: @ccstone, @Shell, @Script, @Number, @Lines, @Condition, @bbedit-talk, @David_Wagner
# -----------------------------------------------------------------------------------------

my $cntr = 0;

while (<>) {
    if (/#Start#/) {
        $cntr++;
    }
    if (/^\d+\h+/) {
        s!^\d+!$cntr!;
    }
    print;
}

# -----------------------------------------------------------------------------------------



--
Best Regards,
Chris

Christopher Stone

unread,
Jan 6, 2022, 7:56:41 AM1/6/22
to BBEdit-Talk
Hey Chris,

This fails on my Mojave system with the stock `awk`.



#Start#Start#Start#
2 In the beginning, God created the heavens and
Xxxxxxccx
#Start#EndOfGen#Start#
#Start#Start#Start#
6 Thus the heavens and the earth were finished,
Xxxxxxxx
#Start#EndOfGen#Start#
#Start#Start#Start#
10 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
Xxxxxxx
#Start#EndOfGen#Start#



It works if I use the current version of `gawk`.

It looks like David has an older version of macOS as well.

--
Take Care,
Chris


David G Wagner

unread,
Jan 6, 2022, 9:07:18 AM1/6/22
to bbe...@googlegroups.com
Well, not sure what is going on. Here is the three chapters of data after I run the corrected Text Filter. Before I executed your script, I executed Mormdliz line endings to just make sure though I am only working on an Mac or related Apple items.

I was going to send you the actual three chapters, but it will go to the whole group which I think is too much data.

If you want to send email to wagsw...@yahoo.com and I will send you two txt files with input and the output I get from executing the script.  If not, that is done and will it work it out differently.

Again thanks for your time and effort to help. It is much appreciated...

Wags ;)
WagsWorld
Hebrews 4:15
Ph(primary) : 408-914-1341
Ph(secondary): 408-761-7391
<script.jpg>

Chris

unread,
Jan 6, 2022, 2:17:03 PM1/6/22
to bbe...@googlegroups.com
David, 

I suspect Chris S. is correct and we’re using different OS versions and so different versions of awk. 

Looks like I sent you down a rabbit hole. 

Definitely try his Perl script and let us know if it worked. 

--Chris(topher)?

On Jan 6, 2022, at 6:07 AM, David G Wagner <wagsw...@gmail.com> wrote:



David G Wagner

unread,
Jan 6, 2022, 4:53:27 PM1/6/22
to BBEdit-Talk
Chris S, I tried your script, but nothing appears to happen. No changes.

I made mods to your code. I was not as clear as I should have been on explanation desired results.

What I am working on is 50 chapters which are of the format:
#start#
1 xxxxxx
2 xxxxxxx
….
n xxxxxxx
#endofGen#
What I am trying to do is after I find a start, then the next line should be a 1 and text. I want to change that 1 to whatever the current chapter number it should be. All the chapters ar in order. Example for 20 chapter, the output would look like:

#start#
20 xxxxxx
2 xxxxxxx
….
n xxxxxxx
#endofGen#

That line starting with a 1 and following a start is the only change desired.

I am on MacOS 10.15.7 and the very latest BBEdit 14.0.5. The Perl I  am executing is 5.28.2.

I get no errors when I run the the filter.

Here is what I have as a script after my minor changes:

my $cntr = 0;
my $ChgSw = 0;
while (<>) {
    if ( /^#Start#/i ) {
        $cntr++;
        $ChgSw = 1;
     }
    if ( $ChgSw ) {
        if ( /^1\h/ ) {
            s!^1!$cntr!;
         }
        $ChgSw = 0;
     }
    print;
}

Sorry for delay in getting back to you. I truly appreciate the insight from what I see in the scripts and the way they work with BBEdit.

Wags ;)
WagsWorld
Hebrews 4:15
Ph(primary) : 408-914-1341
Ph(secondary): 408-761-7391
--
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.

jj

unread,
Jan 7, 2022, 5:13:04 AM1/7/22
to BBEdit Talk
Hi David,

Here is another option with Javascript for automation this time.

Copy this snippet into the Script Editor, choose Javascript as the language in the languages menu under the window titlebar and run it on BBEdit's front window.

    function run() {
        var chapterNumber = 1;
        let vDocument = Application('BBEdit').textWindows()[0].documents()[0]
        vDocument.contents = vDocument.contents().replace(/#start#\s+\d*\s*/ig, () => "#start#\n" + (chapterNumber++) + " ")
    }
   
HTH

Jean Jourdain

Christopher Stone

unread,
Jan 7, 2022, 6:37:44 AM1/7/22
to BBEdit-Talk
On Jan 06, 2022, at 15:32, David G Wagner <wagsw...@gmail.com> wrote:
I made mods to your code. I was not as clear as I should have been on explanation desired results.


Hey David,

Using your input data sample my script exactly replicates your sample of desired output data format...

Your code does not work on the posted input data sample.

So what in fact does the actual input data look like?  Verbatim?

--
Best Regards,
Chris



Data Sample Provided by David Wagner:



#Start#
1 In the beginning, God created the heavens and
Xxxxxxccx
#EndOfGen#
#Start#
1 Thus the heavens and the earth were finished,
Xxxxxxxx
#EndOfGen#
#Start#
1 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
Xxxxxxx
#EndOfGen#



Result when Perl Text Filter by CCS is run:



#Start#
1 In the beginning, God created the heavens and
Xxxxxxccx
#EndOfGen#
#Start#
2 Thus the heavens and the earth were finished,
Xxxxxxxx
#EndOfGen#
#Start#
3 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
Xxxxxxx
#EndOfGen#



Media Mouth

unread,
Jan 7, 2022, 7:27:50 AM1/7/22
to BBEdit Talk
Hi JJ,

Great to see someone using JXA
I've been using AppleScript forever and have never found a reliable, authoritative reference for how to write for the JavaScript version other than just googling around for examples.
Frankly even finding good resources for regular AppleScript has been a challenge. I've never been able to make much use of the Dictionaries.

Any ideas how to learn more about both?

David G Wagner

unread,
Jan 7, 2022, 4:13:17 PM1/7/22
to BBEdit Talk
Chris S,
Here is the data that I am working with. Used red text to delineate the data being processed. To me the testing is correct as that is pretty much how I would do it with Perl, but nothing was updated… ;)

All data below this line
#start#
1 In the beginning, God created the heavens and the earth. 2 The earth was without form and void, and darkness was over the face of the deep. And the Spirit of God was hovering over the face of the waters.
3 And God said, Let there be light, and there was light. 4 And God saw that the light was good. And God separated the light from the darkness. 5 God called the light Day, and the darkness he called Night. And there was evening and there was morning, the first day.
6 And God said, Let there be an expanse in the midst of the waters, and let it separate the waters from the waters. 7 And God made the expanse and separated the waters that were under the expanse from the waters that were above the expanse. And it was so. 8 And God called the expanse Heaven. And there was evening and there was morning, the second day.
9 And God said, Let the waters under the heavens be gathered together into one place, and let the dry land appear. And it was so. 10 God called the dry land Earth, and the waters that were gathered together he called Seas. And God saw that it was good.
11 And God said, Let the earth sprout vegetation, plants yielding seed, and fruit trees bearing fruit in which is their seed, each according to its kind, on the earth. And it was so. 12 The earth brought forth vegetation, plants yielding seed according to their own kinds, and trees bearing fruit in which is their seed, each according to its kind. And God saw that it was good. 13 And there was evening and there was morning, the third day.
14 And God said, Let there be lights in the expanse of the heavens to separate the day from the night. And let them be for signs and for seasons, and for days and years, 15 and let them be lights in the expanse of the heavens to give light upon the earth. And it was so. 16 And God made the two great lights the greater light to rule the day and the lesser light to rule the night and the stars. 17 And God set them in the expanse of the heavens to give light on the earth, 18 to rule over the day and over the night, and to separate the light from the darkness. And God saw that it was good. 19 And there was evening and there was morning, the fourth day.
20 And God said, Let the waters swarm with swarms of living creatures, and let birds fly above the earth across the expanse of the heavens. 21 So God created the great sea creatures and every living creature that moves, with which the waters swarm, according to their kinds, and every winged bird according to its kind. And God saw that it was good. 22 And God blessed them, saying, Be fruitful and multiply and fill the waters in the seas, and let birds multiply on the earth. 23 And there was evening and there was morning, the fifth day.
24 And God said, Let the earth bring forth living creatures according to their kinds livestock and creeping things and beasts of the earth according to their kinds. And it was so. 25 And God made the beasts of the earth according to their kinds and the livestock according to their kinds, and everything that creeps on the ground according to its kind. And God saw that it was good.
26 Then God said, Let us make man in our image, after our likeness. And let them have dominion over the fish of the sea and over the birds of the heavens and over the livestock and over all the earth and over every creeping thing that creeps on the earth.
27 So God created man in his own image,
in the image of God he created him;
male and female he created them.
28 And God blessed them. And God said to them, Be fruitful and multiply and fill the earth and subdue it, and have dominion over the fish of the sea and over the birds of the heavens and over every living thing that moves on the earth. 29 And God said, Behold, I have given you every plant yielding seed that is on the face of all the earth, and every tree with seed in its fruit. You shall have them for food. 30 And to every beast of the earth and to every bird of the heavens and to everything that creeps on the earth, everything that has the breath of life, I have given every green plant for food. And it was so. 31 And God saw everything that he had made, and behold, it was very good. And there was evening and there was morning, the sixth day.
#EndOfGen#
#Start#
1 Thus the heavens and the earth were finished, and all the host of them. 2 And on the seventh day God finished his work that he had done, and he rested on the seventh day from all his work that he had done. 3 So God blessed the seventh day and made it holy, because on it God rested from all his work that he had done in creation.
The Creation of Man and Woman
4 These are the generations
of the heavens and the earth when they were created,
in the day that the LORD God made the earth and the heavens.
5 When no bush of the field was yet in the land and no small plant of the field had yet sprung up for the LORD God had not caused it to rain on the land, and there was no man to work the ground, 6 and a mist was going up from the land and was watering the whole face of the ground 7 then the LORD God formed the man of dust from the ground and breathed into his nostrils the breath of life, and the man became a living creature. 8 And the LORD God planted a garden in Eden, in the east, and there he put the man whom he had formed. 9 And out of the ground the LORD God made to spring up every tree that is pleasant to the sight and good for food. The tree of life was in the midst of the garden, and the tree of the knowledge of good and evil.
10 A river flowed out of Eden to water the garden, and there it divided and became four rivers. 11 The name of the first is the Pishon. It is the one that flowed around the whole land of Havilah, where there is gold. 12 And the gold of that land is good; bdellium and onyx stone are there. 13 The name of the second river is the Gihon. It is the one that flowed around the whole land of Cush. 14 And the name of the third river is the Tigris, which flows east of Assyria. And the fourth river is the Euphrates.
15 The LORD God took the man and put him in the garden of Eden to work it and keep it. 16 And the LORD God commanded the man, saying, You may surely eat of every tree of the garden, 17 but of the tree of the knowledge of good and evil you shall not eat, for in the day that you eat of it you shall surely die.
18 Then the LORD God said, It is not good that the man should be alone; I will make him a helper fit for him. 19 Now out of the ground the LORD God had formed every beast of the field and every bird of the heavens and brought them to the man to see what he would call them. And whatever the man called every living creature, that was its name. 20 The man gave names to all livestock and to the birds of the heavens and to every beast of the field. But for Adam there was not found a helper fit for him. 21 So the LORD God caused a deep sleep to fall upon the man, and while he slept took one of his ribs and closed up its place with flesh. 22 And the rib that the LORD God had taken from the man he made into a woman and brought her to the man. 23 Then the man said,
 This at last is bone of my bones
and flesh of my flesh;
she shall be called Woman,
because she was taken out of Man.
24 Therefore a man shall leave his father and his mother and hold fast to his wife, and they shall become one flesh. 25 And the man and his wife were both naked and were not ashamed.

#EndOfGen#
#Start#
1 Now the serpent was more crafty than any other beast of the field that the LORD God had made.
He said to the woman, Did God actually say, You shall not eat of any tree in the garden ? 2 And the woman said to the serpent, We may eat of the fruit of the trees in the garden, 3 but God said, You shall not eat of the fruit of the tree that is in the midst of the garden, neither shall you touch it, lest you die. 4 But the serpent said to the woman, You will not surely die. 5 For God knows that when you eat of it your eyes will be opened, and you will be like God, knowing good and evil. 6 So when the woman saw that the tree was good for food, and that it was a delight to the eyes, and that the tree was to be desired to make one wise, she took of its fruit and ate, and she also gave some to her husband who was with her, and he ate. 7 Then the eyes of both were opened, and they knew that they were naked. And they sewed fig leaves together and made themselves loincloths.
8 And they heard the sound of the LORD God walking in the garden in the cool of the day, and the man and his wife hid themselves from the presence of the LORD God among the trees of the garden. 9 But the LORD God called to the man and said to him, Where are you? 10 And he said, I heard the sound of you in the garden, and I was afraid, because I was naked, and I hid myself. 11 He said, Who told you that you were naked? Have you eaten of the tree of which I commanded you not to eat? 12 The man said, The woman whom you gave to be with me, she gave me fruit of the tree, and I ate. 13 Then the LORD God said to the woman, What is this that you have done? The woman said, The serpent deceived me, and I ate.
14 The LORD God said to the serpent,
 Because you have done this,
cursed are you above all livestock
and above all beasts of the field;
on your belly you shall go,
and dust you shall eat
all the days of your life.
15 I will put enmity between you and the woman,
and between your offspring and her offspring;
he shall bruise your head,
and you shall bruise his heel.
16 To the woman he said,
 I will surely multiply your pain in childbearing;
in pain you shall bring forth children.
Your desire shall be contrary to your husband,
but he shall rule over you.
17 And to Adam he said,
 Because you have listened to the voice of your wife
and have eaten of the tree
of which I commanded you,
 You shall not eat of it,
cursed is the ground because of you;
in pain you shall eat of it all the days of your life;
18 thorns and thistles it shall bring forth for you;
and you shall eat the plants of the field.
19 By the sweat of your face
you shall eat bread,
till you return to the ground,
for out of it you were taken;
for you are dust,
and to dust you shall return.
20 The man called his wife's name Eve, because she was the mother of all living. 21 And the LORD God made for Adam and for his wife garments of skins and clothed them.
22 Then the LORD God said, Behold, the man has become like one of us in knowing good and evil. Now, lest he reach out his hand and take also of the tree of life and eat, and live forever 23 therefore the LORD God sent him out from the garden of Eden to work the ground from which he was taken. 24 He drove out the man, and at the east of the garden of Eden he placed the cherubim and a flaming sword that turned every way to guard the way to the tree of life.
#EndOfGen#
All data above this line

Wags ;)
WagsWorld
Hebrews 4:15
Ph(primary) : 408-914-1341
Ph(secondary): 408-761-7391
--
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.

Bruce Van Allen

unread,
Jan 7, 2022, 6:39:10 PM1/7/22
to bbe...@googlegroups.com
Hey David,

David G Wagner wrote on 2022-01-07 1:10 PM:
> Here is the data that I am working with.
>
> All data below this line
> #start#


Note that the very first "#start#" starts with a lower-case 's', while
the rest start with upper-case 'S';

Fixing that, try this as a BBEdit text filter (between the '###'s:

###
#!/usr/bin/env perl

my $incr = 1;
local $/ = "#Start#\n";

while (<>) {
s/^1/$incr++/es;
print;
}
###

This sets the "end of line" to "#Start#\n", which puts the number 1 as
the first character of the next line. Within the loop over these
"lines", the filter substitutes an incrementing number for that '1'.

HTH

--
    - Bruce

_bruce__van_allen__santa_cruz__ca_

David G Wagner

unread,
Jan 7, 2022, 9:32:23 PM1/7/22
to bbe...@googlegroups.com
Correct. That does work. But what if it could be either start or Start. In what you have, what would you have to change for either of those cases?

Wags ;)
WagsWorld
Hebrews 4:15
Ph(primary) : 408-914-1341
Ph(secondary): 408-761-7391

Bruce Van Allen

unread,
Jan 7, 2022, 9:51:43 PM1/7/22
to bbe...@googlegroups.com


David G Wagner wrote on 2022-01-07 3:52 PM:
> Correct. That does work. But what if it could be either start or Start.
> In what you have, what would you have to change for either of those cases?

That end-of-line separator has to be a string, not a regular expression,
so it's fixed. But this will work if you make the string "tart#\n" like
this:

###
#!/usr/bin/env perl

my $incr = 1;
local $/ = "tart#\n";

while (<>) {
s/^1/$incr++/es;
print;
}
###






> On Jan 7, 2022, 15:39 -0800, bbe...@googlegroups.com, wrote:
>>
>> #!/usr/bin/env perl
>>
>> my $incr = 1;
>> local $/ = "#Start#\n";
>>
>> while (<>) {
>> s/^1/$incr++/es;
>> print;
>> }
>
> --
> 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
> <mailto:bbedit+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bbedit/c6413eac-2d31-4c13-88a5-bc500fdc80a1%40Spark
> <https://groups.google.com/d/msgid/bbedit/c6413eac-2d31-4c13-88a5-bc500fdc80a1%40Spark?utm_medium=email&utm_source=footer>.


--
    - Bruce

_bruce__van_allen__santa_cruz__ca_

Christopher Stone

unread,
Jan 9, 2022, 2:44:34 PM1/9/22
to BBEdit-Talk
On Jan 07, 2022, at 15:10, David G Wagner <wagsw...@gmail.com> wrote:
Here is the data that I am working with.


Hey David,

This is why it's so important to provide real-world data samples – both input and output – when asking for help to process text.

Here is a very robust method that lets you use a regular expression to split the text records.

The case of the split-string in insensitive and is also standardized in the file.

--
Best Regards,
Chris


#!/usr/bin/env perl
# -----------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/01/09 13:36
# dMod: 2022/01/09 13:36
# Task: Number Lines Based On A Condition.
# Tags: @ccstone, @Shell, @Script, @Number, @Lines, @Condition, @bbedit-talk, @David_Wagner
# -----------------------------------------------------------------------------------------

my $cntr    = 1; 
my $recSep  = "#Start#";                # Pseudo Input-Record-Separator
local $/    = 0777;                     # Set Actual IRS to Slurp-Mode

$_ = <>;                                # Slurp up all the text
$_ =~ s!^$recSep!$recSep!gim;           # Standardize #Start# case
my @array = split /^$recSep\h*/im, $_;  # Split the text into an array

for my $element (@array) {              # Loop through the array and replace
    $element =~ s!^1(?=\h)!$cntr++!em;  # the first instance of “^1” with $cntr
}

$, = $recSep;                           # Set output-record-separator
print @array;

# -----------------------------------------------------------------------------------------

Reply all
Reply to author
Forward
0 new messages