# Script to split a text document by page count? (Perl, applescript)

**URL:** <https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701>\
**Category:** Factual Questions\
**Created:** [February 27, 2010, 11:26pm UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701 "2010-02-27T23:26:16Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![Stoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/stoid/32/272_2.png) [@Stoid](https://boards.straightdope.com/u/Stoid)\
**Post date:** [March 2, 2010, 12:40am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/21 "2010-03-02T00:40:37Z")

</div>

by the way, is the **./** the command to invoke any Perl script, or is this something specific to this?

---

<div class="post-metadata">

**Author:** ![Stoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/stoid/32/272_2.png) [@Stoid](https://boards.straightdope.com/u/Stoid)\
**Post date:** [March 2, 2010, 12:45am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/22 "2010-03-02T00:45:30Z")

</div>

Beowulff: I tried it completely stripped down:

> [@](#):
>
> Stoidbook:ptest Stoidyhome$ csplit 40ccba.txt ‘/\<!DOCTYPE/’  
> 0  
> 278713

Same thing: two files, one empty, one a repeat of the original.

ARGH!!!

What was I doing wrong with the “line\_no” command?

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [March 2, 2010, 1:08am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/23 "2010-03-02T01:08:19Z")

</div>

> [@Stoid](#):
>
> Beowulff: I tried it completely stripped down:
> 
> Same thing: two files, one empty, one a repeat of the original.
> 
> ARGH!!!
> 
> What was I doing wrong with the “line\_no” command?

If this is what you typed, it assumes that the file is at the root of your hard drive - is it?  
You need to specify the full path to the file, which is what the dragging and dropping does.

---

<div class="post-metadata">

**Author:** ![Stoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/stoid/32/272_2.png) [@Stoid](https://boards.straightdope.com/u/Stoid)\
**Post date:** [March 2, 2010, 2:20am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/24 "2010-03-02T02:20:22Z")

</div>

> [@beowulff](#):
>
> If this is what you typed, it assumes that the file is at the root of your hard drive - is it?  
> You need to specify the full path to the file, which is what the dragging and dropping does.

I’ve done it both ways, I was getting directory problems at first and sorted that out. It won’t do anything at all if it can’t find it. As long as it’s doing something, even if it’s wrong, I know that the directory isn’t the problem.

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [March 2, 2010, 2:26am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/25 "2010-03-02T02:26:33Z")

</div>

> [@Stoid](#):
>
> I’ve done it both ways, I was getting directory problems at first and sorted that out. It won’t do anything at all if it can’t find it. As long as it’s doing something, even if it’s wrong, I know that the directory isn’t the problem.

One issue may be line endings. Unix expects linefeed "  
". If you are using \<cr\>, then you might have problems with csplit.

If you want to PM me and send me the file, I can try it over here.

---

<div class="post-metadata">

**Author:** ![Stoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/stoid/32/272_2.png) [@Stoid](https://boards.straightdope.com/u/Stoid)\
**Post date:** [March 2, 2010, 2:51am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/26 "2010-03-02T02:51:52Z")

</div>

> [@beowulff](#):
>
> One issue may be line endings. Unix expects linefeed "  
> ". If you are using \<cr\>, then you might have problems with csplit.

I mean absolutely no offense whatsoever… but can I blow you now? (I’m a woman, in case that helps. )

YOU RULE!!!

That was the problem!!! WOOHOO!!!

I just saved it with Unix line endings via TextWrangler, and VOILA!

Now I have to try the rename script… WOOHOO

(That is an enormously important thing for me to understand for future reference… awesome. YAY!)

---

<div class="post-metadata">

**Author:** ![Stoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/stoid/32/272_2.png) [@Stoid](https://boards.straightdope.com/u/Stoid)\
**Post date:** [March 2, 2010, 3:06am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/27 "2010-03-02T03:06:01Z")

</div>

> [@Erasmus\_Darwin](#):
>
> Standard caveats apply: It was a quick-and-dirty script. It doesn’t do much in the way of error checking. If it gets confused, it’ll either leave the file unrenamed (xx##) or rename it to something oddly long (40ccba1234-50ccba-5678.htm). The former would happen if it can’t find any picture names at all, and the latter would happen if you have 2 different prefixes on your pictures.

Part one is finally working. So now I’m trying to get your script working (which is a wonderful thing because it is based on the ACTUAL contents of the file, not the assumed contents, which my long-way-round is, and could screw up) and this is what’s happening so far…

> [@](#):
>
> mypoot:ptest mypoot$ ./rename\_files.pl xx\*  
> -bash: ./rename\_files.pl: Permission denied  
> mypoot:ptest mypoot$./rename\_files.pl xx  
> -bash: ./rename\_files.pl: Permission denied  
> mypoot:ptest mypoot$chmod 755 rename\_files.pl  
> mypoot:ptest mypoot$ ./rename\_files.pl xx\*  
> mypoot:ptest mypoot$ ./rename\_files.pl xx  
> Error reading xx: No such file or directory at ./rename\_files.pl line 8.  
> mypoot:ptest mypoot$

Because I am so incredibly clueless about all this, I didn’t know for sure where to put the rename\_files script, I assumed it should be in the same folder as the split files, so that’s where it is.

and my files are indeed named xxdigitdigit, so I also assume your xx\* means “all the files starting with xx” teh asterisk being a wildcard.

I’m so close…

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [March 2, 2010, 3:07am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/28 "2010-03-02T03:07:09Z")

</div>

> [@Stoid](#):
>
> I mean absolutely no offense whatsoever… but can I blow you now? (I’m a woman, in case that helps. )
> 
> YOU RULE!!!
> 
> That was the problem!!! WOOHOO!!!
> 
> I just saved it with Unix line endings via TextWrangler, and VOILA!
> 
> Now I have to try the rename script… WOOHOO
> 
> (That is an enormously important thing for me to understand for future reference… awesome. YAY!)

Glad that I could help.  
As far as blowjobs go…let me ask my wife.

\<Hon, I just helped some chick on the SDMB, and she wants to show her gratitude in a very special way - is that OK?\>

Sorry, Stoid, I’m afraid that my wife says that BJs are just not an acceptable form of gratitude indication. But, I _ **really** _ appreciate the thought.😛

---

<div class="post-metadata">

**Author:** ![Erasmus\_Darwin](https://avatars.discourse-cdn.com/v4/letter/e/c57346/32.png) [@Erasmus\_Darwin](https://boards.straightdope.com/u/Erasmus_Darwin)\
**Post date:** [March 2, 2010, 4:06am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/29 "2010-03-02T04:06:14Z")

</div>

> [@Stoid](#):
>
> by the way, is the **./** the command to invoke any Perl script, or is this something specific to this?

./ tells it to run a command in the current directory. If you have ‘.’ in your PATH, then it’s unnecessary. However, a lot of UNIX-based systems don’t have ‘.’ in the PATH for security reasons. I have no clue what OS X does.

> [@Stoid](#):
>
> I’m so close…

Actually, I think you’re done. Once you did the chmod 755 and ran it again, it looks like it worked. Do an ‘ls’ and see what the directory looks like now. Once it worked, it gave you an error when you tried it again as all the xx files had been renamed.

---

<div class="post-metadata">

**Author:** ![Stoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/stoid/32/272_2.png) [@Stoid](https://boards.straightdope.com/u/Stoid)\
**Post date:** [March 2, 2010, 4:32am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/30 "2010-03-02T04:32:49Z")

</div>

> [@Erasmus\_Darwin](#):
>
> Actually, I think you’re done. Once you did the chmod 755 and ran it again, it looks like it worked. Do an ‘ls’ and see what the directory looks like now. Once it worked, it gave you an error when you tried it again as all the xx files had been renamed.

Sad to say, nope.

I ran the script through some debug tools and the same errors kept coming back. The syntax was ok, but it keeps spitting out the line 8 issue, saying:

“Error reading xx: No such file or directory at ./rename\_files.pl line 8.”

I only get the error when I try it with xx standing alone. When I put xx\* it does nothing, no error, no changes, nothing. See: (I tried renaming the docs rrdoc and adding the extension… just in case.)

> [@](#):
>
> mypoot:test myhome$ ls  
> Working docs xx23 xx49 xx75  
> rename\_files.pl xx24 xx50 xx76  
> split\_by\_doctype.pl xx25 xx51 xx77  
> xx00 xx26 xx52 xx78  
> xx01 xx27 xx53 xx79  
> xx02 xx28 xx54 xx80  
> xx03 xx29 xx55 xx81  
> xx04 xx30 xx56 xx82  
> xx05 xx31 xx57 xx83  
> xx06 xx32 xx58 xx84  
> xx07 xx33 xx59 xx85  
> xx08 xx34 xx60 xx86  
> xx09 xx35 xx61 xx87  
> xx10 xx36 xx62 xx88  
> xx11 xx37 xx63 xx89  
> xx12 xx38 xx64 xx90  
> xx13 xx39 xx65 xx91  
> xx14 xx40 xx66 xx92  
> xx15 xx41 xx67 xx93  
> xx16 xx42 xx68 xx94  
> xx17 xx43 xx69 xx95  
> xx18 xx44 xx70 xx96  
> xx19 xx45 xx71 xx97  
> xx20 xx46 xx72 xx98  
> xx21 xx47 xx73 xx99  
> xx22 xx48 xx74  
> mypoot:test myhome$ ./rename\_files.pl xx\*  
> mypoot:test myhome$ ./rename\_files.pl xx44  
> mypoot:test myhome$ ./rename\_files.pl /Users/myhome/Desktop/test/xx44  
> mypoot:test myhome$ id  
> uid=501(myhome) gid=501(myhome) groups=501(myhome),98(\_lpadmin),81(\_appserveradm),101(com.apple.access\_screensharing),79(\_appserverusr),80(admin),102(com.apple.access\_ssh)  
> mypoot:test myhome$ pwd  
> /Users/myhome/Desktop/test  
> mypoot:test myhome$ ls -l rename\_files.pl  
> -rwxr-xr-x@ 1 myhome myhome 566 Mar 1 19:26 rename\_files.pl  
> mypoot:test myhome$ -d rename\_files.pl  
> -bash: -d: command not found  
> mypoot:test myhome$ ./rename\_files.pl xx\*  
> mypoot:test myhome$ ./rename\_files.pl xx  
> Error reading xx: No such file or directory at ./rename\_files.pl line 8.  
> mypoot:test myhome$ ./rename\_files.pl xx44  
> mypoot:test myhome$ perl rename\_files.pl xx\*  
> mypoot:test myhome$ ./rename\_files.pl /Users/myhome/Desktop/test/xx17  
> mypoot:test myhome$ ./rename\_files.pl xx17  
> mypoot:test myhome$ ./rename\_files.pl rrdoc\*  
> mypoot:test myhome$ ./rename\_files.pl rrdoc  
> Error reading rrdoc: No such file or directory at ./rename\_files.pl line 8.  
> mypoot:test myhome$ ls  
> Working docs rrdoc24.txt rrdoc50.txt rrdoc76.txt  
> rename\_files.pl rrdoc25.txt rrdoc51.txt rrdoc77.txt  
> rrdoc00.txt rrdoc26.txt rrdoc52.txt rrdoc78.txt  
> rrdoc01.txt rrdoc27.txt rrdoc53.txt rrdoc79.txt  
> rrdoc02.txt rrdoc28.txt rrdoc54.txt rrdoc80.txt  
> rrdoc03.txt rrdoc29.txt rrdoc55.txt rrdoc81.txt  
> rrdoc04.txt rrdoc30.txt rrdoc56.txt rrdoc82.txt  
> rrdoc05.txt rrdoc31.txt rrdoc57.txt rrdoc83.txt  
> rrdoc06.txt rrdoc32.txt rrdoc58.txt rrdoc84.txt  
> rrdoc07.txt rrdoc33.txt rrdoc59.txt rrdoc85.txt  
> rrdoc08.txt rrdoc34.txt rrdoc60.txt rrdoc86.txt  
> rrdoc09.txt rrdoc35.txt rrdoc61.txt rrdoc87.txt  
> rrdoc10.txt rrdoc36.txt rrdoc62.txt rrdoc88.txt  
> rrdoc11.txt rrdoc37.txt rrdoc63.txt rrdoc89.txt  
> rrdoc12.txt rrdoc38.txt rrdoc64.txt rrdoc90.txt  
> rrdoc13.txt rrdoc39.txt rrdoc65.txt rrdoc91.txt  
> rrdoc14.txt rrdoc40.txt rrdoc66.txt rrdoc92.txt  
> rrdoc15.txt rrdoc41.txt rrdoc67.txt rrdoc93.txt  
> rrdoc16.txt rrdoc42.txt rrdoc68.txt rrdoc94.txt  
> rrdoc17.txt rrdoc43.txt rrdoc69.txt rrdoc95.txt  
> rrdoc18.txt rrdoc44.txt rrdoc70.txt rrdoc96.txt  
> rrdoc19.txt rrdoc45.txt rrdoc71.txt rrdoc97.txt  
> rrdoc20.txt rrdoc46.txt rrdoc72.txt rrdoc98.txt  
> rrdoc21.txt rrdoc47.txt rrdoc73.txt rrdoc99.txt  
> rrdoc22.txt rrdoc48.txt rrdoc74.txt split\_by\_doctype.pl  
> rrdoc23.txt rrdoc49.txt rrdoc75.txt  
> mypoot:test myhome$

---

<div class="post-metadata">

**Author:** ![Erasmus\_Darwin](https://avatars.discourse-cdn.com/v4/letter/e/c57346/32.png) [@Erasmus\_Darwin](https://boards.straightdope.com/u/Erasmus_Darwin)\
**Post date:** [March 2, 2010, 4:52am UTC](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701/31 "2010-03-02T04:52:42Z")

</div>

Huh. It looks like it’s running, but it’s not picking up the photo URLs to allow it to determine the new filename. It’s specifically looking for something like:  
HREF=“photos/\*\*\*\*\*.jpg”  
(where \*\*\*\*\* represents arbitrary text).

If your photo links are different, then it won’t pick up the filename. It seemed to work okay when I tested it with the sample page you posted.

[Previous page](https://boards.straightdope.com/t/script-to-split-a-text-document-by-page-count-perl-applescript/530701.md?page=1)
