Including a number in a text item delimiter

I have a text document that I’d like to delimit by "1 - " where the number is a random number followed by a space, a hyphen, and then another space.

I cannot for the life of me figure out how to do that.

Any help would be appreciated.

Thanks.

Dan

Hi Dan,

text item delimiters can contain every character which class is one of the text classes

dsr:

You could do it this way:


set AppleScript's text item delimiters to (((random number from 1 to 10 as integer) & " -") as Unicode text)

Bear in mind that if you apply this to a block of text only one random number will apply unless you want to loop through items.


set oldTID to AppleScript's text item delimiters

set pretendTextBlock to "This is a paragraph one.
This is paragraph two.
this is paragraph three.
This is paragraph four."

set AppleScript's text item delimiters to (((random number from 1 to 10 as integer) & " - ") as Unicode text)
set pretendTextBlock to (every paragraph of pretendTextBlock) as Unicode text

set AppleScript's text item delimiters to oldTID

pretendTextBlock
-- returns: "This is a paragraph one.4 - This is paragraph two.4 - this is paragraph three.4 - This is paragraph four."

I’m curious as to what you’d want to delimit with this.

Regards,
Jim Neumann
BLUEFROG

Stefan-

I don’t quite follow your post.

BLUEFROG-

I knew I’d word that slightly wrong.

The number isn’t actually random per se. It’s a different number at each instance ie: "1 - "; "2 - "; etc. So I need to delimit by (number & " - ").

I’m searching through a text file archive of cds. Each new cd entry begins with "number - " and then has listings of the contents.

Thank you both for the quick reply.

I have noticed that the numbers are no longer numbers once they’re read from the file. That would be why I can’t delimit simply with

set applescript's text item delimiters to {(number) & " -" }

Supposing your source text looks like ‘source’:

set source to "1 - Foo
Track 1
Track 2
Track 3
2 - Bar
Track 1
Track 2
3 - Foobar
Track 1
Track 2
Track 3
Track 4
Track 5
Track 6"
set temp to do shell script "echo " & (quoted form of source) & " | perl -pe 's/^\\d+ - //;s/\\d+ - /#¢#/g'"
set {old_delim, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "#¢#"}
set temp to text items of temp
set {AppleScript's text item delimiters, old_delim} to {old_delim, missing value}
return temp

Actually, this is what the source text looks like:

1 - Jobs 2006_DVD51 -29425664 / 0
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramBol.AFM (MacWrite document)
2/7/87 12/11/87 15195 TEXT MACA
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramBolIta.AFM (MacWrite document)
8/12/87 12/11/87 15528 TEXT MACA
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramLig.AFM (MacWrite document)
11/24/87 12/11/87 15303 TEXT MACA
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramLigIta.AFM (MacWrite document)
8/12/87 12/11/87 15435 TEXT MACA
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramBol (Downloadable font)
2/7/87 12/11/87 45495 LWFN ASPF
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramBolIta (Downloadable font)
7/10/87 12/11/87 47108 LWFN ASPF
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramLig (Downloadable font)
11/24/87 12/11/87 47033 LWFN ASPF
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramLigIta (Downloadable font)
7/10/87 12/11/87 45200 LWFN ASPF
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:Garamond Bitmapped Fonts (Font Suitcase)
11/24/87 7/12/96 110061 FFIL DMOV
Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:HammerFat:HammeFat (Downloadable font)
4/14/92 4/14/92 16537 LWFN ACp1

The next entry would begin just like 1 - Jobs… above but with “2 - (whatever)”

I think I’m going to be able to get what I need with grep:

set theVariable to do shell script "grep -o '[[:digit:]]*[[:space:]][-][[:space:]]'"

Then I should be able to change the text after setting it to a variable, right?

If you just want to filter out everything buth the album titles then this should be quite okay for a start.

I was thinking of taking the results of the grep search, replacing them with a string of characters I’m sure isn’t in the text document, and then delimiting by that.

Thank you all for your help by the way. :slight_smile:

Dan

Well, that’s just what my last script snippet was doing. :P:cool:


--just changed your sample text to this for testing purposes:
set source to "1 - Jobs 2006_DVD51    -29425664 / 0
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramBol.AFM    (MacWrite document)
        2/7/87    12/11/87    15195    TEXT    MACA
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramBolIta.AFM    (MacWrite document)
        8/12/87    12/11/87    15528    TEXT    MACA
2 - Jobs 2006_DVD51    -29425664 / 0
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramLig.AFM    (MacWrite document)
        11/24/87    12/11/87    15303    TEXT    MACA
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:AFM Files:GaramLigIta.AFM    (MacWrite document)
        8/12/87    12/11/87    15435    TEXT    MACA
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramBol    (Downloadable font)
        2/7/87    12/11/87    45495    LWFN    ASPF
3 - Jobs 2006_DVD51    -29425664 / 0
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramBolIta    (Downloadable font)
        7/10/87    12/11/87    47108    LWFN    ASPF
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramLig    (Downloadable font)
        11/24/87    12/11/87    47033    LWFN    ASPF
4 - Jobs 2006_DVD51    -29425664 / 0
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:GaramLigIta    (Downloadable font)
        7/10/87    12/11/87    45200    LWFN    ASPF
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:Garamond:Garamond Bitmapped Fonts    (Font Suitcase)
        11/24/87    7/12/96    110061    FFIL    DMOV
    Jobs 2006_DVD51:016261 Hull Tractor Pull:Fonts:HammerFat:HammeFat    (Downloadable font)
        4/14/92    4/14/92    16537    LWFN    ACp1"

set temp to do shell script "echo " & (quoted form of source) & " | perl -pe 's/\\d+ - /#¢#/g'"
set {old_delim, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "#¢#"}
set temp to items 2 thru -1 of text items of temp
set {AppleScript's text item delimiters, old_delim} to {old_delim, missing value}
return temp

I thought it kinda’ looked like that, but the Perl (that is Perl right?) script threw me…

I will put “…with mucho help from Vincent” at the top of the finished sript. :stuck_out_tongue:

Oh, there’s no need for a credit :wink: We’re here to help :cool:

It is much appreciated.