<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>ByteLabs: Replace Newlines with Sed</title>
    <link>http://blog.solaris.bytelabs.org/articles/2009/05/01/replace-newlines-with-sed</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>additions to a vast pool of entropy</description>
    <item>
      <title>Replace Newlines with Sed</title>
      <description>&lt;p&gt;This has bugged me for a while when useing TextMates &amp;#8216;Filter Through Command&amp;#8230;&amp;#8217; to replace all newlines in a selection. Somehow my mind always wants to use &lt;strong&gt;sed&lt;/strong&gt; to do this although there are &lt;a href="http://steve.vbboys.com/blog/?p=47"&gt;simpler ways to do it&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;The following works with &lt;a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?sed"&gt;&lt;span class="caps"&gt;GNU&lt;/span&gt; sed&lt;/a&gt; but not with &lt;a href="http://developer.apple.com/documentation/Darwin/Reference/Manpages/man1/sed.1.html"&gt;&lt;span class="caps"&gt;BSD&lt;/span&gt; sed&lt;/a&gt; on my Mac (&lt;em&gt;although it should work given the documentation in the &lt;a href="http://developer.apple.com/documentation/Darwin/Reference/Manpages/man1/sed.1.html"&gt;man&lt;/a&gt; page&lt;/em&gt;):&lt;/p&gt;


&lt;pre&gt;
sed -n ':a;N;$!ba;s/\n//g;p' &amp;lt; infile
&lt;/pre&gt;

What this does is the following:
	&lt;ul&gt;
	&lt;li&gt;&lt;em&gt;:a&lt;/em&gt; put label at the first position&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;N&lt;/em&gt; Append the next line of input to the pattern space&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;$!ba&lt;/em&gt; branch to label &lt;strong&gt;a&lt;/strong&gt;, the ! tells it to apply the command to a non-selected pattern space and the $ addresses the last line of input&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;s/\n//g&lt;/em&gt; substitutes a newline with a space&lt;/li&gt;
		&lt;li&gt;&lt;em&gt;p&lt;/em&gt; writes pattern space to standard output&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;If you can make this work with &lt;span class="caps"&gt;BSD&lt;/span&gt; sed please let me know.&lt;/p&gt;</description>
      <pubDate>Fri, 01 May 2009 13:03:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:2076c4d5-f696-4ef7-b381-e2080e0e7553</guid>
      <author>igor</author>
      <link>http://blog.solaris.bytelabs.org/articles/2009/05/01/replace-newlines-with-sed</link>
      <category>Fun Stuff</category>
      <category>Hacking and Computers</category>
    </item>
  </channel>
</rss>
