<?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>Huddled Masses &#187; Import-Csv</title>
	<atom:link href="http://huddledmasses.org/tag/import-csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerShell Convert Delimiters, Import Tab Delimited Text&#8230;</title>
		<link>http://huddledmasses.org/powershell-convert-delimiters-import-tab-delimited-text/</link>
		<comments>http://huddledmasses.org/powershell-convert-delimiters-import-tab-delimited-text/#comments</comments>
		<pubDate>Sun, 07 Oct 2007 05:12:14 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Import-Csv]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-convert-delimiters-import-tab-delimited-text/</guid>
		<description><![CDATA[Well, just for a change, lets cut straight to the script ( 5pm EST Sunday): Function Convert-Delimited&#40;&#91;regex&#93;$from,&#91;string&#93;$to&#41; &#123; &#160; process &#160; &#123; &#160; &#160; $_ = $_ -replace &#34;(?:`&#34;((?:(?:[^`&#34;]&#124;`&#34;`&#34;))+)(?:`&#34;$from&#124;`&#34;`$))&#124;(?:((?:.(?!$from))*.)(?:$from&#124;`$))&#34;,&#34;Þ`$1`$2Þ$to&#34; &#160; &#160; $_ = $_ -replace &#34;Þ(?:$to&#124;Þ)?`$&#34;,&#34;Þ&#34; &#160; &#160; $_ = $_ -replace &#34;`&#34;`&#34;&#34;,&#34;`&#34;&#34; -replace &#34;`&#34;&#34;,&#34;`&#34;`&#34;&#34; &#160; &#160; $_ = $_ -replace &#34;Þ((?:[^Þ`&#34;](?!$to))+)Þ($to&#124;`$)&#34;,&#34;`$1`$2&#34; &#160; &#160; $_ [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, just for a change, lets cut straight to the script ( <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>5pm <span class="caps">EST</span> Sunday</strong>):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">Function</span> <span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">Delimited</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$from</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$to</span><span style="color: #333;">&#41;</span> <br />
<span style="color: #333;">&#123;</span> <br />
&nbsp; <span style="color: #666699; font-weight: bold;">process</span><br />
&nbsp; <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;(?:<span style="color: #000099; font-weight: bold;">`&quot;</span>((?:(?:[^<span style="color: #000099; font-weight: bold;">`&quot;</span>]|<span style="color: #000099; font-weight: bold;">`&quot;</span><span style="color: #000099; font-weight: bold;">`&quot;</span>))+)(?:<span style="color: #000099; font-weight: bold;">`&quot;</span>$from|<span style="color: #000099; font-weight: bold;">`&quot;</span><span style="color: #000099; font-weight: bold;">`$</span>))|(?:((?:.(?!$from))*.)(?:$from|<span style="color: #000099; font-weight: bold;">`$</span>))&quot;</span>,<span style="color: #009900;">&quot;Þ<span style="color: #000099; font-weight: bold;">`$</span>1<span style="color: #000099; font-weight: bold;">`$</span>2Þ$to&quot;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;Þ(?:$to|Þ)?<span style="color: #000099; font-weight: bold;">`$</span>&quot;</span>,<span style="color: #009900;">&quot;Þ&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span><span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span>,<span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span>,<span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span><span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;Þ((?:[^Þ<span style="color: #000099; font-weight: bold;">`&quot;</span>](?!$to))+)Þ($to|<span style="color: #000099; font-weight: bold;">`$</span>)&quot;</span>,<span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`$</span>1<span style="color: #000099; font-weight: bold;">`$</span>2&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;Þ&quot;</span>,<span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;Þ&quot;</span>,<span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Basically, this is a set of regular expressions that can be run in the pipeline to convert delimited text from one delimiter to another. If the new delimiter needs quotes added to make it work correctly, the converter will do so (actually, it adds a marker character Þ around every field, and then removes them where they are not needed, and replaces them with quotes where they are &#8212; long story, and I welcome any improvements).</p>

	<p>Just as an example, assume a column-based text file like this Hosts.txt file (notice that the columns are separated by two <strong>or more</strong> spaces, since there are spaces within the text columns):</p>

	<div class="txt code txt" style="font-family:monospace;"><br />
Host &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Standout Features &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Languages &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Space/Bandwidth &nbsp; Monthly<br />
BlueHost &nbsp; &nbsp; &nbsp;PostgreSQL + MySQL, Free Cert &nbsp; &nbsp; &nbsp; PHP5,Perl,Python,Ruby &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 300GB/3TB &nbsp; &nbsp; &nbsp;.95/mo for 2yrs<br />
DreamHost &nbsp; &nbsp; Jabber,SVN,WebDAV &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PHP4,PHP5,Perl,Python,Ruby &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500GB/5TB &nbsp; &nbsp; &nbsp;.95/mo for 2yrs<br />
NetFirms Adv &nbsp;2 domains, Shared Cert &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PHP4,PHP5,Perl,Ruby,Python &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;250GB/2TB &nbsp; &nbsp; &nbsp;.95/yr with &quot;995&quot; code<br />
NetFirms Bus &nbsp;Windows + Linux, MsSQL, 5 domains &nbsp; ASP,ASP.NET,PHP4,PHP5,Perl,Ruby,Python &nbsp;300GB/3TB &nbsp; &nbsp; &nbsp;.95/mo for 3mo</div>

	<p>You can convert it to <span class="caps">CSV</span> by using this function, and write it back out to file like this: <code>Get-Content Hosts.txt | Convert-Delimiter &#34; + &#34; &#34;,&#34; | Set-Content Hosts.csv</code>.  Now, since my original problem was all about importing the files I also went ahead and wrote a slightly more magical function called Import-Delimited:<span id="more-441"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">Function</span> <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Delimited</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$delimiter</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;,&quot;</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$PsPath</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;&quot;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">begin</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$script</span>:tmp <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">Path</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">GetTempFileName</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">debug</span></span> <span style="color: #009900;">&quot;Using tempfile $($script:tmp)&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">Function</span> <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">String</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$inputString</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$inputString</span>.<span style="color: #003366;">Length</span> <span style="color: #000066;">-gt</span> <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">debug</span></span> <span style="color: #009900;">&quot;Importing $inputString&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$inputString</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">FileInfo</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Exists</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> <span style="color: #660033; font-weight: bold;">$inputString</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">Delimiters</span></span> <span style="color: #660033; font-weight: bold;">$delimiter</span> <span style="color: #009900;">&quot;,&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Content</span></span> <span style="color: #660033; font-weight: bold;">$script</span>:tmp<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$pwd</span> <span style="color: #660033; font-weight: bold;">$inputString</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>IO.<span style="color: #003366;">FileInfo</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Exists</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$pwd</span> <span style="color: #660033; font-weight: bold;">$inputString</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">Delimiters</span></span> <span style="color: #660033; font-weight: bold;">$delimiter</span> <span style="color: #009900;">&quot;,&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Content</span></span> <span style="color: #660033; font-weight: bold;">$script</span>:tmp<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$inputString</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">Delimiters</span></span> &nbsp;<span style="color: #660033; font-weight: bold;">$delimiter</span> <span style="color: #009900;">&quot;,&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Content</span></span> <span style="color: #660033; font-weight: bold;">$script</span>:tmp<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">debug</span></span> <span style="color: #009900;">&quot;Nothing to Import&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">String</span></span> <span style="color: #660033; font-weight: bold;">$PsPath</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #666699; font-weight: bold;">process</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">String</span></span> <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">end</span> <br />
&nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Csv</span></span> <span style="color: #660033; font-weight: bold;">$script</span>:tmp<br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Item</span></span> <span style="color: #660033; font-weight: bold;">$script</span>:tmp<br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>This will allow you to <strong>directly import</strong> most delimited text files.  But not only that, it can import text from the pipeline! So you can actually create objects out of any legacy application that outputs delimited text!  Just for example, with our previous text file, we could do any of the following one-liners:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$hosts</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Delimited</span></span> <span style="color: #009900;">&quot; + &quot;</span> Hosts.<span style="color: #003366;">txt</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$hosts</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> Hosts.<span style="color: #003366;">txt</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Delimited</span></span> <span style="color: #009900;">&quot; + &quot;</span> <br />
<br />
<span style="color: #660033; font-weight: bold;">$hosts</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Content</span></span> Hosts.<span style="color: #003366;">txt</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">Delimiters</span></span> <span style="color: #660033; font-weight: bold;">$delimiter</span> <span style="color: #009900;">&quot;,&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Delimited</span></span> <span style="color: #666666; font-style: italic;"># CSV is the default</span></div>

	<p>Any questions?  Here&#8217;s the <a href='http://HuddledMasses.org/wordpress/wp-content/uploads/2007/10/import-delimited.ps1' title='Import-Delimited function'>pair of functions</a> as a single script download.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<h5>Question 1: What&#8217;s with the &#8220; + &#8220;?</h5>

	<p>It&#8217;s a regular expression meaning one or more spaces followed by a space (ie: two or more spaces).</p>

	<h5>Question 2: Regular expressions? How do I just use this to import a tab-delimited text file?</h5>

	<p>The regular expressions for separators should be pretty simple, really.  For instance, a tab-delimited text file would just be &#8220;`t&#8221; and a comma-delimited file is just a comma: &#8220;,&#8221; ... if you wanted to make sure to get rid of any spaces around the commas, you could use &#8220; +, +&#8221; but we should mostly be taking care of that already.</p>

	<h5>Question 3: I get weird errors about the value of argument &#8220;name&#8221; being invalid?</h5>

	<p>Import-Delimited actually uses Import-Csv in the background, and requires a header row which has a name for each column: You can&#8217;t have a two separators in a row, or a trailing separator (although the scripts try to clean that up).  Incidentally, this is a really good point: you <strong>must</strong> have the first row be the delimited headers, otherwise your first object will be treated as the header row.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-convert-delimiters-import-tab-delimited-text/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

