<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>tag:blog.danieljanus.pl,2019:category:unix</id>
  <title>Daniel Janus – Unix</title>
  <link href="http://blog.danieljanus.pl/category/unix/"/>
  <updated>2010-02-16T00:00:00Z</updated>
  <author>
    <name>Daniel Janus</name>
    <uri>http://danieljanus.pl</uri>
    <email>dj@danieljanus.pl</email>
  </author>
  <entry>
    <id>tag:blog.danieljanus.pl,2010-02-16:post:downcasing</id>
    <title>Downcasing strings</title>
    <link href="http://blog.danieljanus.pl/downcasing/"/>
    <updated>2010-02-16T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;I just needed to convert a big (around 200 MB) text file, encoded in UTF-8 and containing Polish characters, all into lowercase. &lt;code&gt;tr&lt;/code&gt; to the rescue, right? Well, not quite.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;$ echo ŻŹŚÓŃŁĘĆĄ | tr A-ZĄĆĘŁŃÓŚŹŻ a-ząćęłńóśźż
żźśóńłęćą
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Looks reasonable (apart from the fact that I need to specify an explicit character mapping — it would be handy to just have a lcase utility or suchlike); but here’s what happens on another random string:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;$ echo abisyński | tr A-ZĄĆĘŁŃÓŚŹŻ a-ząćęłńóśźż
abisyŅski
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I was just about to report this as a bug, when I spotted the following in the manual:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Currently &lt;code&gt;tr&lt;/code&gt; fully supports only single-byte characters. Eventually it will support multibyte characters; when it does, the &lt;code&gt;-C&lt;/code&gt; option will cause it to complement the set of characters, whereas &lt;code&gt;-c&lt;/code&gt; will cause it to complement the set of values.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Turns out some of the basic tools don’t support multibyte encodings. &lt;code&gt;dd conv=lcase&lt;/code&gt;, for instance, doesn’t even pretend to touch non-ASCII letters, and perl’s &lt;code&gt;tr&lt;/code&gt; operator likewise fails miserably even when one specifies &lt;code&gt;use utf8&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;This is a sad, sad state of affairs. It’s 2010, UTF-8 has been around for seventeen years, and it’s still not supported by one of the core operating system components as other encodings are becoming more and more obsolete. I’m dreaming of the day my system uses it internally for everything.&lt;/p&gt;&lt;p&gt;Fortunately, not everything is broken. Gawk, for example, works:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;$ echo koŃ i żÓłw | gawk '{ print tolower($0); }'
koń i żółw
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and so does sed.&lt;/p&gt;&lt;p&gt;&lt;em&gt;Update 2010-04-04:&lt;/em&gt; I should have been more specific. The above rant applies to the GNU tools (&lt;code&gt;tr&lt;/code&gt; and &lt;code&gt;dd&lt;/code&gt;) as found in most Linux distributions; other versions can be more featureful. As &lt;a href="http://alexott.net/"&gt;Alex Ott&lt;/a&gt; points out in an email comment, tr on OS X works as expected for characters outside of ASCII, and also supports character classes as in &lt;code&gt;tr '[:upper:]' '[:lower:]'&lt;/code&gt;. This is yet another testimony to general high quality of Apple software; in this particular case, though, it may well be a direct effect of OS X’s BSD heritage. Does it work on *BSD?&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
  <entry>
    <id>tag:blog.danieljanus.pl,2008-06-11:post:mind-the-symlinks</id>
    <title>Today’s lesson: Mind the symlinks</title>
    <link href="http://blog.danieljanus.pl/mind-the-symlinks/"/>
    <updated>2008-06-11T00:00:00Z</updated>
    <content type="html">&lt;div&gt;&lt;p&gt;Probably every day I keep learning new things, without even realizing it most of the time. The vast majority of them are minor or even tiny tidbits of knowledge; but even these might be worth noting down from time to time, especially when they are tiny pitfalls I’d fallen into and spent a couple of minutes getting out. By sharing them, I might hopefully prevent someone else for slipping and falling in.&lt;/p&gt;&lt;p&gt;So here’s a simple Unix question: If you enter a subdirectory of the current directory and back to &lt;code&gt;..&lt;/code&gt;, where will you end up? The most obvious answer is, of course, “in the original directory”, and is mostly correct. But is it always? Let’s see.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;nathell@breeze:~$ pwd
/home/nathell
nathell@breeze:~$ cd foobar
nathell@breeze:~/foobar$ cd ..
nathell@breeze:~$ pwd
/home/nathell
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So the hypothesis seems to be right. But let’s try doing this in Python, just for the heck of it:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;nathell@breeze:~$ python
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import os
&gt;&gt;&gt; print os.getcwd()
/home/nathell
&gt;&gt;&gt; os.chdir("foobar")
&gt;&gt;&gt; os.chdir("..")
&gt;&gt;&gt; print os.getcwd()
/var
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Whoa, hang on! What’s that &lt;code&gt;/var&lt;/code&gt; doing there? Of course the one thing I didn’t tell you is that &lt;code&gt;foobar&lt;/code&gt; is not really a directory, but rather a symlink pointing to one (&lt;code&gt;/var/log&lt;/code&gt; in this case).&lt;/p&gt;&lt;p&gt;The corollary is that the shell builtin &lt;code&gt;cd&lt;/code&gt; is &lt;em&gt;not the same&lt;/em&gt; as Unix &lt;code&gt;chdir()&lt;/code&gt; (it is easily checked that both Perl and C exhibit the same behaviour). In fact, the shell builtin has an oft-forgotten command-line switch, &lt;code&gt;-P&lt;/code&gt;, which causes it to follow physical instead of logical path structure.&lt;/p&gt;&lt;p&gt;On a closing note: I have somewhat neglected the blog throughout the previous month, but I hope to revive it soon. It is not unlikely that such irregularities will recur.&lt;/p&gt;&lt;/div&gt;</content>
  </entry>
</feed>
