# Errors, trying to edit and write some text files

**URL:** <https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567>\
**Category:** Ruby\
**Created:** [September 15, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567 "2007-09-15T16:07:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 15, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/1 "2007-09-15T16:07:28Z")

</div>

Hi, I’m a total ruby noob but i’ve obtained this script from another  
forum over at Tracks. I’m trying to extract some info from a plain text  
feed of my upcoming calendar events and write specific info to three  
different text files (today, tomorrow, and next 7 days). I’m on a Mac.

The script is here: I’ve already made edits specific to my computer (the  
address of my text feed for example):

#!/usr/bin/ruby  
#Â GenerateÂ 3Â seperateÂ textÂ filesÂ fromÂ TracksÂ TXTÂ feeds  
#Â listingÂ actionsÂ dueÂ todayÂ (orÂ overdue),Â tomorrowÂ or  
#Â inÂ theÂ nextÂ 7Â days.  
#Â YouÂ canÂ thenÂ pullÂ theseÂ textÂ filesÂ intoÂ GeekToolÂ andÂ colour  
#Â themÂ separately.

#Â ========Â ConstantsÂ toÂ setÂ withÂ yourÂ valuesÂ ===============

# 

#Â URLÂ forÂ theÂ baseÂ feed:Â [http://yourdomain.tld/feed/text/[token]](http://yourdomain.tld/feed/text/%5Btoken%5D)  
URLÂ =Â “[http://0.0.0.0:3000/feed/text/Nathan/b0b2a7c81680d1a1920b199ecb6db0ad86e2e984](http://0.0.0.0:3000/feed/text/Nathan/b0b2a7c81680d1a1920b199ecb6db0ad86e2e984)”  
#Â PathÂ toÂ temporaryÂ directory  
TEMP\_DIRÂ =Â “/Users/”

# 

#Â =============================================  
#Â GrabÂ eachÂ feed,Â splitÂ intoÂ lines,Â thenÂ getÂ ridÂ ofÂ anyÂ non-actionÂ lines  
todayÂ =Â `curlÂ -sÂ "#{URL}"?due=0`.split(“\n”).select{|line|Â lineÂ =~Â /\s+[Due:/Â orÂ lineÂ =~Â /^\w/}  
tomorrowÂ =Â `curlÂ -sÂ "#{URL}"?due=1`.split(“\n”).select{|line|Â lineÂ =~Â /\s+[Due:/Â orÂ lineÂ =~Â /^\w/}  
this\_weekÂ =Â `curlÂ -sÂ "#{URL}"?due=6`.split(“\n”).select{|line|Â lineÂ =~Â /\s+[Due:/Â orÂ lineÂ =~Â /^\w/}

#Â GetÂ ridÂ ofÂ repeatedÂ actionsÂ inÂ tomorrowÂ andÂ this\_week  
tomorrowÂ =Â tomorrowÂ -Â today  
today\_or\_tomorrowÂ =Â todayÂ +Â tomorrow  
this\_weekÂ =Â this\_weekÂ -Â today\_or\_tomorrow

#Â PrintÂ theÂ infoÂ toÂ threeÂ files,Â storedÂ inÂ TEMP\_DIR  
fileÂ =Â File.new(“#{TEMP\_DIR}/today.txt”,Â “w”)  
Â Â file.printÂ “\nToday:\n"Â +Â today.join(”\n")  
file.close

fileÂ =Â File.new(“#{TEMP\_DIR}/tomorrow.txt”,Â “w”)  
Â Â file.printÂ “\nTomorrow:\n"Â +Â tomorrow.join(”\n")  
file.close

fileÂ =Â File.new(“#{TEMP\_DIR}/later.txt”,Â “w”)  
Â Â file.printÂ “\nLater:\n"Â +Â this\_week.join(”\n")  
file.close

When I run this from BBEdit I get about 50 lines of this:

untitled text:11: Invalid char `\302' in expression untitled text:11: Invalid char `\240’ in expression  
untitled text:11: Invalid char `\302' in expression untitled text:11: Invalid char `\240’ in expression  
untitled text:13: Invalid char `\302' in expression untitled text:13: Invalid char `\240’ in expression  
untitled text:13: Invalid char `\302' in expression untitled text:13: Invalid char `\240’ in expression  
untitled text:17: Invalid char `\302' in expression untitled text:17: Invalid char `\240’ in expression  
untitled text:17: Invalid char `\302' in expression untitled text:17: Invalid char `\240’ in expression

Any ideas? Sorry if this appears obvious. Many thanks.

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/2 "2007-09-17T13:23:34Z")

</div>

Nathan Thickett-menghini [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> untitled text:17: Invalid char `\302' in expression untitled text:17: Invalid char `\240’ in expression  
> untitled text:17: Invalid char `\302' in expression untitled text:17: Invalid char `\240’ in expression

Even in my newsreader program I had trouble with your message. The  
problem seems to be that something went wrong when you did the cut and  
paste from the Tracks forum. Your code is full of invisible characters  
between the normal characters. BBEdit should in fact be able to help you  
track this down and fix the problem. Feel free to email me the BBEdit  
file privately and I’ll remove the gremlins for you if you don’t know  
how to do it. m.

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 17, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/3 "2007-09-17T15:54:04Z")

</div>

Many thanks, I had some success by typing out the script myself and this  
eliminated the troublesome characters. The script now runs and produces  
my three text files. I still can’t get it to work as desired - it seems  
to omit the lines of the calendar that I most need, i.e. the events  
themselves! I don’t understand enough about the script to be able to fix  
it but I think the error comes where the script splits the text feeds  
into separate lines - it only seems to include the first line of the  
feed and never more. If you fancy taking a look then by all means do but  
I appreciate you fixing my first problem.

For example, the feed for ‘today’ is as follows in its entirety (in the  
web browser, before ruby has touched it):

CAR:  
[17/09/2007] Car to Garage at 8.30am

But the ruby script returns only:

Today:  
CAR:

Any ideas?

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 17, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/4 "2007-09-17T22:56:56Z")

</div>

Nathan Thickett-menghini [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For example, the feed for ‘today’ is as follows in its entirety (in the  
> Today:  
> CAR:
> 
> Any ideas?
> 
> Attachments:  
> [http://www.ruby-forum.com/attachment/299/coloured\_actions.rb](http://www.ruby-forum.com/attachment/299/coloured_actions.rb)

Well, the script is doing exactly what you’re telling it to do. This  
expression:

select{|line|line =~ /\s+[Due:/ or line =~ /^\w/}

…means:

Keep only those lines that begin with the word “[Due:” (possibly after  
some whitespace), or that begin with a word character (e.g. “a” or “b”  
or “c”…).

Well, the first line does begin with a word character (“C” for “CAR:”),  
so it’s kept. But the second line does not - it begins with whitespace,  
and the whitespace is not followed by the word “[Due:”, so it’s thrown  
away.

So it seems as if the script you’ve adopted is inappropriate to the  
format of the actual data you wish to parse.

m.

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 17, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/5 "2007-09-17T23:03:43Z")

</div>

That’s brilliant thanks! So it seems to me that the Tracks Developers  
have changed the format of the feeds since the script was written,  
omitting the word ‘Due:’. How can I get it to include all lines that  
begin with white space? Many thanks, you’ve been fantastic!

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 18, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/6 "2007-09-18T18:36:48Z")

</div>

Many thanks - you’re helping me hugely. I’ve gone through the script and  
made the changes you suggested and it has worked in the most part. The  
part of the script that parses the lines is now as follows:

today = `curl -s "#{URL}"?due=0`.split("\n").select{|line| line =~  
/\s+/}  
tomorrow = `curl -s "#{URL}"?due=1`.split("\n").select{|line| line =~  
/\s+/}  
this\_week = `curl -s "#{URL}"?due=6`.split("\n").select{|line| line =~  
/\s+/}  
later = `curl -s "#{URL}"?due=365`.split("\n").select{|line| line =~  
/\s+/}

I’m sure this is not perfect but it seems to do the trick of including  
the lines that begin with blank space. Let me show you the feed as you  
requested.

HOME:  
[18/09/2007] Parcelforce coming after 1pm  
[19/09/2007] be a bender  
[21/09/2007] Sort important documents etc

WORK:  
[24/09/2007] Learn NATS Stuff

CAR:  
[30/09/2007] nob

So you see there are lines that begin with letters (these are the  
contexts, Home, Work, etc) lines with a small gap followed by events,  
and lines that are entirely blank.

You’ll have noticed that part of the script deletes duplicate lines -  
so that you don’t get an event that is on today’s feed repeated on the  
weekly feed. This works mostly but it also deletes repeated CONTEXTS,  
i.e. the context ‘HOME:’ will appear on today’s feed but not on any  
others. I’ve tried to get round this by totally abandoning all contexts,  
i.e. changing the part that parses the lines to NOT INCLUDE lines that  
begin with word characters. This works, but it now still includes the  
totally empty lines, which puts gaps in my text files. Could you add a  
tiny part to the following line to show me how to NOT include empty  
lines? Is this possible? I know it seems like a lot of effort for a  
small problem, but I’d really appreciate it, it will make me more  
productive to see my events neatly on my desktop.

today = `curl -s "#{URL}"?due=0`.split("\n").select{|line| line =~  
/\s+/}

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 18, 2007, 4:33am UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/7 "2007-09-18T04:33:43Z")

</div>

Nathan Thickett-menghini [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That’s brilliant thanks! So it seems to me that the Tracks Developers  
> have changed the format of the feeds since the script was written,  
> omitting the word ‘Due:’. How can I get it to include all lines that  
> begin with white space?

It’s hard to say, because I don’t know the format of the feeds. You  
could change the script you have, just deleting the six character phrase  
“[Due:” wherever you see it. But that is just a guess, not knowing what  
the feeds look like. It would be better if you’d show us a multi-line  
feed output so we can see what the desirable and undesirable lines look  
like. In the example you gave, _all_ lines were desirable, which made  
some of the program wasteful. But perhaps in a fuller example, there are  
some undesirable lines. The change I’m suggesting might not eliminate  
them; it depends what they look like.

m.

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [September 19, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/errors-trying-to-edit-and-write-some-text-files/114567/8 "2007-09-19T19:50:45Z")

</div>

Nathan Thickett-menghini [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> begin with word characters. This works, but it now still includes the  
> totally empty lines, which puts gaps in my text files. Could you add a

In my view the entire original script is misconceived. You can’t just  
look at individual lines. You need to cycle thru the lines in order,  
gathering info as categorized. Something, perhaps, like this:

cats = {}  
today = `curl -s "#{URL}"?due=0`  
today.split(“\n”).inject do |memo, line|  
line.lstrip!  
if line =~ /^\w\*😕  
cats[line] = []  
line  
else  
cats[memo] |= line.to\_a  
memo  
end  
end

Now “cats” holds a nice hash where the keys are the categories and each  
category’s value is an array of its events. Do that again for “tomorrow”  
and the others, using the same “cats”; no duplicate values will be  
inserted, because of the use of the |= operator. When you’re all  
finished, output “cats” in whatever format you like, such as:

cats.each do |cat, events|  
puts cat  
events.each do |event|  
puts " " + event  
end  
end

If you wanted to get really neat, you could sort stuff before  
outputting.

m.
