<?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>youdidwhatwithtsql.com &#187; whitespace</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/whitespace/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Wed, 01 Sep 2010 17:02:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Trimming Whitespace with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388</link>
		<comments>http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388#comments</comments>
		<pubDate>Thu, 01 Oct 2009 19:14:11 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[whitespace]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388</guid>
		<description><![CDATA[A few days ago I was working with a client that was providing an export of data from Oracle. The file being produced was choking my SSIS package due to various formatting issues. After working with the client and getting a file that looked good to the naked eye I discovered that a large amount [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388">Trimming Whitespace with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p>A few days ago I was working with a client that was providing an export of data from <a href="http://www.oracle.com/index.html" target="_blank">Oracle</a>. The file being produced was choking my <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> package due to various formatting issues. After working with the client and getting a file that looked good to the naked eye I discovered that a large amount of whitespace on the end of each line was making things break at my end.</p>
<p>I need to import this file quickly so I decided to fix it myself. As usual <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> was up to the task! Here’s the script I came up with. This script will process a file called <strong>test.csv</strong> located in e:\ and produce a new file called <strong>test2.csv</strong> with the trailing whitespace removed from each line.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p388code2'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3882"><td class="code" id="p388code2"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$content</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>;
<span style="color: #800080;">$file</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;e:\test.csv&quot;</span>;
<span style="color: #800080;">$count</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Processing file&quot;</span> <span style="color: #008080; font-style: italic;">-CurrentOperation</span> <span style="color: #800000;">&quot;Line = 0&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">0</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Starting&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #800080;">$lines</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$file</span>;
<span style="color: #800080;">$percent_complete</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #008000;"># trim line by line</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$lines</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
 	<span style="color: #800080;">$line</span> <span style="color: pink;">=</span> <span style="color: #800080;">$line</span>.TrimEnd<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
 	<span style="color: #800080;">$content</span> <span style="color: pink;">+=</span> <span style="color: #800000;">&quot;$line<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span> <span style="color: #008000;"># Add a newline</span>
 	<span style="color: #800080;">$count</span><span style="color: pink;">++</span>;
 	<span style="color: #800080;">$percent_complete</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$count</span> <span style="color: pink;">/</span> <span style="color: #800080;">$lines</span>.Count<span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
	 <span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Processing file&quot;</span> <span style="color: #008080; font-style: italic;">-CurrentOperation</span> <span style="color: #800000;">&quot;Line = $count&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$percent_complete</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Running&quot;</span>  <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #800080;">$content</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;e:\test2.csv&quot;</span>;
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Finished processing file&quot;</span> <span style="color: #008080; font-style: italic;">-CurrentOperation</span> <span style="color: #800000;">&quot;All lines processed&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Complete&quot;</span>  <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">5</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/trim_whitespace_from_files_powershell.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="trim_whitespace_from_files_powershell" border="0" alt="trim whitespace from files powershell thumb Trimming Whitespace with Powershell" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/trim_whitespace_from_files_powershell_thumb.png" width="244" height="134" /></a> </p>
<p>After this quick fix the file was easily imported!</p>
<p><map name='google_ad_map_388_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/388?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_388_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=388&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftrimming-whitespace-with-powershell%2F388' title="Trimming Whitespace with Powershell" alt=" Trimming Whitespace with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388">Trimming Whitespace with Powershell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
