<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reliably Broken &#187; unix</title>
	<atom:link href="http://reliablybroken.com/b/tag/unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://reliablybroken.com/b</link>
	<description>It&#039;s a blog: let&#039;s do funch!</description>
	<lastBuildDate>Thu, 01 Dec 2011 21:27:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Split a file on any character in Python</title>
		<link>http://reliablybroken.com/b/2010/04/split-a-file-on-any-character-in-python/</link>
		<comments>http://reliablybroken.com/b/2010/04/split-a-file-on-any-character-in-python/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 10:38:12 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=437</guid>
		<description><![CDATA[I need to split a big text file on a certain character. I expect I am being thick about this, but split doesn&#8217;t quite do what I want because it includes the matching line, whereas I want to split right on the matching character. My Python answer: def readlines(filename, endings, chunksize=4096): """Returns a generator that [...]]]></description>
			<content:encoded><![CDATA[<p>I need to split a big text file on a certain character. I expect I am being thick about this, but <a href="http://developer.apple.com/Mac/library/documentation/Darwin/Reference/ManPages/man1/split.1.html"><code>split</code></a> doesn&#8217;t quite do what I want because it includes the matching line, whereas I want to split right on the matching character.</p>

<p>My Python answer:</p>

<pre><code>def readlines(filename, endings, chunksize=4096):
    """Returns a generator that splits on lines in a file with the given
    line-ending.
    """
    line = ''
    while True:        
        buf = filename.read(chunksize)
        if not buf:
            yield line
            break

        line = line + buf

        while endings in line:
            idx = line.index(endings) + len(endings)
            yield line[:idx]
            line = line[idx:]

if __name__ == "__main__":
    import sys, os

    FORMFEED = chr(12) # ASCII 12
    basename = os.path.basename(sys.argv[1])
    for num, data in enumerate(readlines(open(sys.argv[1]), endings=FORMFEED)):
        filename = basename + '-' + str(num)
        open(filename, 'wb').write(data)
</code></pre>

<p>This is also useful when reading data exported from some old-fashioned Mac application like <a href="http://www.filemaker.com/support/downloads/downloads_prev_versions.html">Filemaker 5</a> where the line-endings are ASCII 13 not ASCII 10.</p>

<p>This post was inspired by <a href="http://www-01.ibm.com/software/lotus/products/notes/">Lotus Notes</a> version 8.5, which is so advanced that to save a message in a file on disk you have to export it as structured text. And if you want to save a whole bunch of messages as individual files you must forget that <a href="http://www.mactech.com/articles/mactech/Vol.10/10.06/DragAndDrop/index.html">drag-and-drop was introduced with System 7</a>, that would be too obvious.</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2010/04/split-a-file-on-any-character-in-python/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Removing printing restrictions in 10.5</title>
		<link>http://reliablybroken.com/b/2010/01/10-5-printing-restrictions/</link>
		<comments>http://reliablybroken.com/b/2010/01/10-5-printing-restrictions/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 17:38:32 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=384</guid>
		<description><![CDATA[I am trying to write a good one-liner for removing all restrictions on printing for Mac OS X 10.5. I had thought that sed would be perfect for this, but I can&#8217;t arrive at a simple syntax for appending new lines that works well when pasted into a terminal window. Here&#8217;s what I ended up [...]]]></description>
			<content:encoded><![CDATA[<p>I am trying to write a good one-liner for removing all restrictions on printing for Mac OS X 10.5. I had thought that <a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/sed.1.html"><code>sed</code></a> would be perfect for this, but I can&#8217;t arrive at a simple syntax for appending new lines that works well when pasted into a terminal window. Here&#8217;s what I ended up with:</p>

<pre><code>perl -p -0 -i '.bak' -e 's/(Policy default).*(Policy)/$1&gt;\n&lt;Limit All&gt;\nOrder deny,allow\nAllow from all\n&lt;\/Limit&gt;\n&lt;$2/s' /private/etc/cups/cupsd.conf
</code></pre>

<p>Rather brutal, it just guts the default policy and replaces it with the following:</p>

<pre><code>&lt;Policy default&gt;
&lt;Limit All&gt;
Order deny,allow
Allow from all
&lt;/Limit&gt;
&lt;/Policy&gt;
</code></pre>

<p>Greg Neagle has <a href="http://www.mactech.com/articles/mactech/Vol.24/24.06/2406MacEnterprise-LeopardPrinting/index.html">a useful article about printing in the enterprise</a>. Apple suggests <a href="http://support.apple.com/kb/HT3511">adding the network group to the local lpadmin group</a>, but points out that mobile users would need to be added individually. In my case most accounts are mobile accounts and we trust everyone to manage print queues on a Mac, so removing all restrictions is acceptable.</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2010/01/10-5-printing-restrictions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using screen to log a terminal session</title>
		<link>http://reliablybroken.com/b/2009/01/using-screen-to-log-a-terminal-session/</link>
		<comments>http://reliablybroken.com/b/2009/01/using-screen-to-log-a-terminal-session/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 21:58:55 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://reliablybroken.com/b/?p=27</guid>
		<description><![CDATA[Note to self: use screen when doing a bunch of remote administration stuff over an SSH connection. You can tell it to log the session to a separate file, and can detach the session and log off without having the remote shell be terminated. Then later you can resume the session and haven&#8217;t lost anything. [...]]]></description>
			<content:encoded><![CDATA[<p>Note to self: <a href="http://www.gnu.org/software/screen/">use screen</a> when doing a bunch of remote administration stuff over an SSH connection. You can tell it to log the session to a separate file, and can detach the session and log off without having the remote shell be terminated. Then later you can resume the session and haven&#8217;t lost anything.</p>

<p>To start a shell and log everything to a file, do</p>

<pre><code>screen -L -S mysession
</code></pre>

<p>That drops you into a new shell, and lets you refer to that session later (in case you are ambitious and have many screen sessions running simultaneously). To detach the session type <code>CTRL+a</code> then <code>d</code>. From there you can exit cleanly. To resume the session later, type</p>

<pre><code>screen -r mysession
</code></pre>

<p>The session history will be saved to a file named <code>screenlog.0</code> or similar in the directory where screen was first invoked.</p>

<p><a href="http://www.manpagez.com/man/1/screen/">screen man page online</a> (Hmmm&#8230; looks like the hmug man pages have renamed themselves.)</p>

<p>I should use <code>screen</code> more often. I should also get in the habit of going through the contents of $PATH every time Mac OS X gets updated.</p>
]]></content:encoded>
			<wfw:commentRss>http://reliablybroken.com/b/2009/01/using-screen-to-log-a-terminal-session/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

