Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Split text files for iPod Notes usage via Perl UNIX
I've created a simple Perl script that splits a text file into 4KB parts to use within iPod Notes. Usage is pretty simple:
  1. Copy and paste the script (macosxhints mirror) into a plain text file named makenotes.pl on your system. Remember to make the script executable (chmod 755 makenotes.pl).
  2. Put the script and the text you want to split into a directory. For this example, assume the directory is named book and located in your user's home folder.
  3. Open up a Terminal and go to that directory: cd ~/book
  4. Run the script with the filename (or filenames) as arguments: ./makenotes.pl colourofmagic.txt
When run, the script will ask you to create a title for this book -- this title is used as a directory name and as a prefix for the parts. For instance, if I use the title colour_of_magic, the script will create a directory named colour_of_magic in the current directory, and put the parts into that folder, eg colour_of_magic_1.txt, colour_of_magic_2.txt, colour_of_magic/colour_of_magic_3.txt, etc.

[robg adds: I haven't tested this one.]
    •    
  • Currently 1.80 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[10,478 views]  

Split text files for iPod Notes usage via Perl | 6 comments | Create New Account
Click here to return to the 'Split text files for iPod Notes usage via Perl' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Split text files for iPod Notes usage via Perl
Authored by: windrag on Jul 22, '08 08:47:18AM

Great! I've been looking for something like this for a while. Is there any way to bundle this in an Automator droplet so you could just drag and drop your text file? I'm not quite sure how to tell Automator how to apply the script to the text file.

Thanks!

---
windrag | 2.4GHz MacBook Pro Core 2 Duo | 160GB HD | 2GB RAM | OS X 10.5.4



[ Reply to This | # ]
Split text files for iPod Notes usage via Perl
Authored by: windrag on Jul 22, '08 03:10:01PM

Actually, I just tried to run this script with the instructions given and I get the following error:

<code>-bash: ./makenotes.pl: /usr/bin/perl^M: bad interpreter: No such file or directory</code>

---
windrag | 2.4GHz MacBook Pro Core 2 Duo | 160GB HD | 2GB RAM | OS X 10.5.4



[ Reply to This | # ]
Split text files for iPod Notes usage via Perl
Authored by: windrag on Jul 22, '08 08:30:27PM
OK, so I figured out the problem (the command perl is what runs the script, like this:

perl ./makenotes.pl colourofmagic.txt

Probably obvious to most, but I'm not a regular perl user. So, I got it to work, but I'm used to using UTF-8 text files, and this script returns a bunch of improperly translated characters. I changed my text file encoding to Western (Mac OS Roman) and the output looks great, except that the first page is just one character. Anyway, thanks again!

---
windrag | 2.4GHz MacBook Pro Core 2 Duo | 160GB HD | 2GB RAM | OS X 10.5.4

[ Reply to This | # ]

Split text files for iPod Notes usage via Perl
Authored by: mjohnson2 on Jul 22, '08 09:44:38PM

Based on that error message, it looks like maybe you might have transferred that file between Windows and Unix/Linux in ASCII mode and introduced a CRLF problem. I'm making that guess based on the "^M" included in the error message. On Unix, we typically remove that with a sed command: 's/^V^M//g' (where ^V^M are done simultaneously and so only appears as ^M). You could also try transferring again in BINARY mode.



[ Reply to This | # ]
Split text files for iPod Notes usage via Perl
Authored by: Anonymous on Jul 24, '08 01:33:03PM

I see you like Terry Pratchett. Can I pass off your script as my own?

Surely the solution to reading long text files is Safari? I don't have an Iphone, but it's the same browser, right?



[ Reply to This | # ]
Split text files for iPod Notes usage via Perl
Authored by: serme on Sep 03, '08 11:34:56PM

I'm new to perl, but I used your script as the basis for my own, which looks for a line break and then a word break to optimize space without cutting out mid-word. Limitations: The title has a max length of 15 chars (107 bytes for markup + 3954 for text = 4051 bytes; title is repeated in 3 markups).

unless (@ARGV) {
print "Usage: $0 <filename(s)>\n";
exit(1);
}

foreach my $file (@ARGV) {
if (-f $file) {
print "$file exists.\n";
&chopfile($file);
} else {
print "$file not found.\n";
}
}

sub chopfile {
my $filename = shift;
open(my $longfile, "<", $filename) || die ("can't open $filename");
my @arr = ();
my $len = 0;
my $i = 0;
print "Title:> \n";
my $title = <STDIN>;
chomp($title);
mkdir($title);
chdir($title);
while (<$longfile>) {
my $trylen = length($_);
if (($len + $trylen) <= 3954) {
push(@arr, $_);
$len += $trylen;
} else {
my $diff = 3954 - $len;
my $addme = substr($_, 0, rindex($_, " ", $diff));
my $addmelater = substr($_, (rindex($_, " ", $diff) + 1));
push(@arr, $addme);
open(my $output, ">", $title.$i.".txt");
print $output "<?xml encoding=\"utf-8\"?>\n";
print $output "<TITLE>".$i." ".$title."</TITLE>\n";
print $output "<A HREF=\"".$title.($i-1).".txt\" NOPUSH><</A>\n" unless ($i == 0);
print $output @arr;
print $output "\n<A HREF=\"".$title.($i+1).".txt\" NOPUSH>></A>";
close $output;
@arr = $addmelater;
$len = length($addmelater);
print $title.$i." created.\n" if (($i % 10) == 1);
$i++;
}
open(my $output, ">", $title.$i.".txt");
print $output "<?xml encoding=\"utf-8\"?>\n";
print $output "<TITLE>".$i." ".$title."</TITLE>\n";
print $output "<A HREF=\"".$title.($i-1).".txt\" NOPUSH><</A>\n" unless ($i == 0);
print $output @arr;
close $output;
}
}



[ Reply to This | # ]