<?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>Joe Maller &#187; sed</title>
	<atom:link href="http://joemaller.com/tag/sed/feed/" rel="self" type="application/rss+xml" />
	<link>http://joemaller.com</link>
	<description>.com</description>
	<lastBuildDate>Fri, 27 Jan 2012 06:04:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Fixing a Palm duplicate disaster</title>
		<link>http://joemaller.com/888/fixing-a-palm-duplicate-disaster/</link>
		<comments>http://joemaller.com/888/fixing-a-palm-duplicate-disaster/#comments</comments>
		<pubDate>Sun, 17 Feb 2008 07:17:05 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[misc.]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[Palm]]></category>
		<category><![CDATA[Palm Desktop]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[vcard]]></category>

		<guid isPermaLink="false">http://joemaller.com/2008/02/17/fixing-a-palm-duplicate-disaster/</guid>
		<description><![CDATA[I recently came across an absolute disaster of a Palm Desktop data file while helping someone setup a new iPhone. It had 13,572 contacts, mostly duplicates. Judging from the number of obvious duplicate entries, my guess is the actual number will be somewhere around 2500 (it was). Here is the process I used to automatically [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across an absolute disaster of a Palm Desktop data file while helping someone setup a new iPhone. It had 13,572 contacts, mostly duplicates. Judging from the number of obvious duplicate entries, my guess is the actual number will be somewhere around 2500 (it was).</p>
<p>Here is the process I used to automatically remove a lot of those duplicates and import the remainder into the Mac&#8217;s Address Book. </p>
<p>The first step is to get out of Palm Desktop as soon as possible. Select all contacts and export to a group VCard. This one was 3.4 MB.</p>
<p>Most of this will happen in Terminal, but a quick stop in BBEdit or <a href="http://www.barebones.com/products/textwrangler/">TextWrangler</a> will save a few steps later on. (TextMate tends to choke on big, non-UTF files.) The Palm export file is encoded in MacRoman. It&#8217;s 2008, pretty much any text that isn&#8217;t Unicode should be. I used TextWrangler to convert the encoding to UTF-8 no BOM (byte order marker).</p>
<p>VCards require Windows style CRLF line endings. While we could deal with those in Sed, we might as well just switch the file to Unix style LF endings in TextWrangler too. The TextWrangler bottom bar should switch from this:  </p>
<p><img src='http://joemaller.com/wordpress/wp-content/uploads/2008/02/macroman_crlf.png' alt='MacRoman CRLF' /></p>
<p>To this:</p>
<p><img src='http://joemaller.com/wordpress/wp-content/uploads/2008/02/utf8_lf.png' alt='utf8 LF' /></p>
<p>Now comes the <a href="http://sed.sourceforge.net/sed1line.txt">magic</a>.</p>
<p>While this could be done as an impossible-to-read one-line sed command, it&#8217;s easier to digest and debug as separate command files.</p>
<p>Here are the steps:</p>
<ol>
<li>Use Sed to join each individual VCard into a single line using a token to replace line feeds, output to intermediate file</li>
<li>Sort and Uniq the result to remove obvious duplicates.
<li>Replace the tokens with line feeds</li>
</ol>
<p>Below are the two sed command files I used. I ran these individually but they could easily be piped together into a one-line command.</p>
<p><em>vcard_oneline.sed:</em></p>
<pre><code># define the range we'll be working with
/BEGIN:VCARD/,/END:VCARD/ {

# define the loopback
:loop

# add the next line to the pattern buffer
N

# if pattern is not found, loopback and add more lines
/\nEND:VCARD$/! b loop

# replace newlines in multi-line pattern
s/\n/   %%%     /g
}</code></pre>
<p>Run that like this:  </p>
<pre><code>sed -f vcard_oneline.sed palm_dump.vcf &gt; vcards_oneline.txt</code></pre>
<p>Then run that file through sort and uniq: </p>
<pre><code>sort vcards_oneline.txt | uniq &gt; vcards_clean.txt </code></pre>
<p><em>vcard_restore.sed:</em></p>
<pre><code># replace tokens with DOS style CRLF line endings
s/      %%%     /^M\
/g

# add the &lt;CR&gt; before the LF at the end of the line
s/$/^M/</code></pre>
<p>Run that with something like this:  </p>
<pre><code>sed -f vcard_restore.sed vcards_clean.txt &gt; vcards_clean.vcf</code></pre>
<p>After that last step, you should be able to drag the vcards_clean.vcf file into Address Book to import your vcards.</p>
<p>Suggestions for improvement are always welcomed.</p>
<h3>Notes:</h3>
<p>In VIM, type the tab character as control-v-i (hold control while pressing v then i), type the <CR> line break by typing control-v-enter.</p>
<p><code>iconv</code> could be used to convert from MacRoman to UTF-8. TextWrangler just seemed easier at the time.</p>
<p>Palm Desktop appears to dump group VCards in input order, so duplicate entries were not grouped together. Running the output through <code>sort</code> visually reveals a ton of duplicates and makes it possible to use <code>uniq</code> to remove consecutive duplicates.</p>
<p>I had to quit and re-open Address Book once or twice before it would import the files.</p>
]]></content:encoded>
			<wfw:commentRss>http://joemaller.com/888/fixing-a-palm-duplicate-disaster/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quick note about sed&#8217;s edit in place option</title>
		<link>http://joemaller.com/823/quick-note-about-seds-edit-in-place-option/</link>
		<comments>http://joemaller.com/823/quick-note-about-seds-edit-in-place-option/#comments</comments>
		<pubDate>Sun, 16 Sep 2007 17:11:33 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[misc.]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://joemaller.com/2007/09/16/quick-note-about-seds-edit-in-place-option/</guid>
		<description><![CDATA[From the sed manpage: -i extension Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situ- ations where disk space is exhausted, etc. [...]]]></description>
			<content:encoded><![CDATA[<p>From the sed manpage:</p>
<pre><code>-i extension
   Edit files in-place, saving backups with the specified extension.
   If a zero-length extension is given, no backup will be saved.  It
   is not recommended to give a zero-length extension when in-place
   editing files, as you risk corruption or partial content in situ-
   ations where disk space is exhausted, etc.</code></pre>
<p>This doesn&#8217;t work:</p>
<p><code>sed -i -e's/apples/oranges/' file.txt</code></p>
<p>The key thing here is that the extension after the <code>-i</code> flag is not optional. If you leave it off, sed assumes you&#8217;ll be entering it via stdin, which isn&#8217;t allowed and yields this error:</p>
<p><code>sed: -i may not be used with stdin</code></p>
<p>The solution is to send a zero-length extension like this:</p>
<p><code>sed -i '' -e's/apples/oranges/' file.txt</code></p>
<p>Careful with this, it could be <strong>really</strong> dangerous with poorly crafted commands.</p>
]]></content:encoded>
			<wfw:commentRss>http://joemaller.com/823/quick-note-about-seds-edit-in-place-option/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>EXIF and the Unix Strings command</title>
		<link>http://joemaller.com/654/exif-and-the-unix-strings-command/</link>
		<comments>http://joemaller.com/654/exif-and-the-unix-strings-command/#comments</comments>
		<pubDate>Mon, 05 Jun 2006 14:55:46 +0000</pubDate>
		<dc:creator>Joe Maller</dc:creator>
				<category><![CDATA[misc.]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[exif]]></category>
		<category><![CDATA[iPhoto]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://joemaller.com/2006/06/05/exif-and-the-unix-strings-command/</guid>
		<description><![CDATA[I got an email over the weekend pointing out a bug in my iPhoto Date Reset if an original image contained a single-quote in its name. Most all of my iPhoto images were imported from the camera, so I hadn&#8217;t seen this before, but I&#8217;m pretty sure I&#8217;ve already gotten it fixed. While fixing that, [...]]]></description>
			<content:encoded><![CDATA[<p>I got an email over the weekend pointing out a bug in my <a href='http://www.joemaller.com/iphoto/'>iPhoto Date Reset</a> if an original image contained a single-quote in its name. Most all of my iPhoto images were imported from the camera, so I hadn&#8217;t seen this before, but I&#8217;m pretty sure I&#8217;ve already gotten it fixed.</p>
<p>While fixing that, I did a little revising of the EXIF sniffing script. I was using a one-line Perl snippet to scrape the date out of the first kilobyte of the file. Here&#8217;s the command broken across several lines</p>
<pre><code> perl -e 'open(IMG, q:[ABSOLUTE PATH}:);
 read(IMG, $exif, 1024);
 $exif =~ s/\n/ /g;
 $exif =~ s/.*([0-9]{4}(?::[0-9]{2}){2} [0-9]{2}(?::[0-9]{2}){2}).*$/$1/g;
 print STDOUT $exif;'</code></pre>
<p>That worked, but perl one-liners usually need to be enclosed in single-quotes, since AppleScript was filling in the path, single-quotes in the name broke the script. I&#8217;m not that fluent in Perl, so there&#8217;re probably better ways of doing that.</p>
<p>But then I stumbled across the Unix <a href='http://www.hmug.org/man/1/strings.php'>Strings</a> command. This basically does most of what I was doing. It scrapes a binary file (meaning non-text) and extracts anything that seems to be a string. The output from JPEGs often contains a bunch of gibberish, but right above the gibberish is every unencoded string from the EXIF header.</p>
<p>Using strings, sed for the pattern and head to trim, that somewhat convoluted perl script became this trim little shell script:</p>
<pre><code> strings [ABSOLUTE PATH] | sed -E -n '/([0-9]{4}(:[0-9]{2}){2} [0-9]{2}(:[0-9]{2}){2})/p' | head -n1</code></pre>
<p>They&#8217;re both essentially instant on my computer so I&#8217;m not going to bother building a test to figure out which is actually faster.</p>
]]></content:encoded>
			<wfw:commentRss>http://joemaller.com/654/exif-and-the-unix-strings-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UTF-8 and high ASCII don&#8217;t mix</title>
		<link>http://joemaller.com/645/utf-8-and-high-ascii-dont-mix/</link>
		<comments>http://joemaller.com/645/utf-8-and-high-ascii-dont-mix/#comments</comments>
		<pubDate>Thu, 20 Apr 2006 14:44:21 +0000</pubDate>
		<dc:creator>Joe Maller</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[ascii]]></category>
		<category><![CDATA[iconv]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://joemaller.com/2006/04/20/utf-8-and-high-ascii-dont-mix/</guid>
		<description><![CDATA[Part of my FXScript compiler works by joining two code chunks with a shell script. Each chunk lives in its own file and contains one &#8220;high-ASCII&#8221; character, a &#169; symbol in one, and a &#8217; (typographically correct apostrophe) in the other. Those are processed with sed and joined with a few additional strings via echo [...]]]></description>
			<content:encoded><![CDATA[<p>Part of my FXScript compiler works by joining two code chunks with a shell script. Each chunk lives in its own file and contains one &#8220;high-ASCII&#8221; character, a &copy; symbol in one, and a &rsquo; (typographically correct apostrophe) in the other. Those are processed with sed and joined with a few additional strings via echo and cat.</p>
<p>For several hours I was stumped because one of the two characters would be garbled after passing through the script.</p>
<p>Finally I noticed that one source file was encoded as ASCII and the other was UTF-8. When both were set to UTF-8, everything worked.</p>
<p>The <a href='http://www.hmug.org/man/1/iconv.php'>iconv</a> command converts files between encodings. I used the following script to covert a directory of ISO-8859-1 Latin1 text files to UTF-8:</p>
<pre><code>for f in *
    do
    cp "$f" "$f.TMP"
    iconv -f LATIN1 -t UTF-8 "$f.TMP" &amp;gt; "$f"
done
rm *.TMP</code></pre>
<p>Here&#8217;s a one-line version:</p>
<pre><code>for f in *; do cp "$f" "$f.TMP"; iconv -f LATIN1 \
-t UTF-8 "$f.TMP" &amp;gt; "$f";  done; rm *.TMP</code></pre>
<p>Just don&#8217;t run that more than once or it will re-convert already converted characters which isn&#8217;t pretty. Iconv doesn&#8217;t buffer data, so attempting to convert in place results in zero-length files. I moved the files first to keep Subversion from freaking out that the files were all new.</p>
<p>As much as it seems like something that should be detectable on the surface, <a href='http://mail.nl.linux.org/linux-utf8/2005-06/msg00004.html'>8-bit text encoding can&#8217;t be sniffed out</a>.</p>
<blockquote><p>
It&#8217;s completely impossible to detect which of the 8-bit encodings is used without any further knowledge (for instance, of the language in use). &#8230;</p>
<p>If you need a formal proof of &#8220;undetectability&#8221;, here&#8217;s one: &#8211; valid ISO-8859-1 string is always completely valid ISO-8859-2 (or -4, -5) string (they occupy exactly the same spots 0xa1-0xff), e.g. you can <em>never</em>  determine if some character not present in another set is actually used.
</p></blockquote>
<p>That&#8217;s the reason I couldn&#8217;t find a counterpart to iconv which would detect and return the encoding of a text file. An alternate solution would be to detect UTF-8 and not reconvert a file that&#8217;s already unicode, but I think I&#8217;m done with this for now.</p>
<p>For a beginning understanding of Unicode and text encoding, start with Joel Spolsky&#8217;s canonical article, <a href='http://www.joelonsoftware.com/articles/Unicode.html'> The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://joemaller.com/645/utf-8-and-high-ascii-dont-mix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Database Caching 1/23 queries in 0.024 seconds using disk: basic
Object Caching 396/448 objects using disk: basic

Served from: joemaller.com @ 2012-02-07 09:54:49 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          tag/sed/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      0.493s
Header info:
Set-Cookie:         bb2_screener_=1328626489+38.107.179.218+38.107.179.218; path=/
X-Pingback:         http://joemaller.com/wordpress/xmlrpc.php
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Tue, 07 Feb 2012 14:54:49 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Tue, 07 Feb 2012 15:54:49 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               f7b78d1eb0984d5b3d9f5ca4e77d68a4
X-Powered-By:       W3 Total Cache/0.9.2.4
Content-Encoding:   gzip
-->
