{"id":17631,"date":"2010-07-27T00:01:00","date_gmt":"2010-07-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/07\/27\/deleting-extra-returns-and-line-feeds-from-a-text-file-using-windows-powershell\/"},"modified":"2010-07-27T00:01:00","modified_gmt":"2010-07-27T00:01:00","slug":"deleting-extra-returns-and-line-feeds-from-a-text-file-using-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/deleting-extra-returns-and-line-feeds-from-a-text-file-using-windows-powershell\/","title":{"rendered":"Deleting Extra Returns and Line Feeds from a Text File Using Windows PowerShell"},"content":{"rendered":"<p>&#160;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/> Hey, Scripting Guy! Is it possible to use <a href=\"http:\/\/support.microsoft.com\/kb\/968929\">Windows PowerShell<\/a> to groom a text file by deleting extra returns and line feeds it may contain? Here is the deal. I have a file that we need to download each day. This file contains updated prices for commodities used in our manufacturing operation. We use the updated commodity pricing to determine our base cost for profit and loss in our general ledger application (g\/l app). A back office application automatically sets our quarterly price and volume discount points based upon the information from the g\/l app. Needless to say, this file is mission critical for us. <\/p>\n<p>In the past, when we tried to automate the downloading and updating process, there were several \u201cincidents.\u201d As a result, our management is skittish about anything related to these applications. The problem is that right now this is a time-consuming manual process. If someone goes on vacation, we have to train a replacement. It is a lot of stress and I would love to automate it using Windows PowerShell. Help!<\/p>\n<p>&#8212; TB<\/p>\n<p>&#160;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/> Hello TB, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. It is so hot and steamy outside that the crickets are hibernating. The dragonflies cannot fly because of the high <a href=\"http:\/\/en.wikipedia.org\/wiki\/Humidity\">humidity<\/a> that saturates their thin wings, making them hang limply like a kite left outside during a spring shower. The birds sit in the shade pleading for a breeze. The rabbits wander aimlessly around the backyard vainly seeking a single green blade of grass for a meal. <\/p>\n<p>Late July in Charlotte offers special opportunities and unique challenges for outdoor activities. Most people seem to shuttle from one air-conditioned spot to another with little thought or attention given to the intermediate scenery. I feel as if I would like to take a nap in the afternoon like I did when I was in Spain. The following photo is one I took in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mijas\">Mijas<\/a> in Southern Spain. Mijas is a really cool old town with narrow streets and lots of interesting shops. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8688.hsg072710011_787826CD.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Photo Ed took in Mijas, Spain\" border=\"0\" alt=\"Photo Ed took in Mijas, Spain\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5633.hsg072710011_thumb_28BEFB84.jpg\" width=\"604\" height=\"442\" \/><\/a> <\/p>\n<p>TB, a text file such as the one shown in the following image has a number of returns and line feeds in it. These are seen in between the paragraphs and at the end of the text file. When importing the data from the text file into a database, each line could introduce a blank record into the database and cause the problems you experienced with your application. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5732.hsg072710021_042D6E00.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Image of example text file with extra returns and line feeds\" border=\"0\" alt=\"Image of example text file with extra returns and line feeds\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1832.hsg072710021_thumb_47956F62.jpg\" width=\"504\" height=\"339\" \/><\/a> <\/p>\n<p>To remove the blank lines in the text file involves several steps.<\/p>\n<ol>\n<li>Rename the text file.<\/li>\n<li>Read the text file.<\/li>\n<li>Find lines with text while ignoring blank lines.<\/li>\n<li>Remove the blank lines from the text.<\/li>\n<li>Write the revised text to a new file.<\/li>\n<li>Save the new file with the name of the original text file.<\/li>\n<\/ol>\n<p>The reason for the roundabout procedure is that, after the text file is opened, a lock will remain on the file that prevents overwriting the file with new content. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1030.hsg072710031_42B2BBA6.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Image of file preventing overwriting\" border=\"0\" alt=\"Image of file preventing overwriting\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1351.hsg072710031_thumb_4D7012FB.jpg\" width=\"604\" height=\"279\" \/><\/a> <\/p>\n<p>To rename the text file, use the <b>Rename-Item<\/b> cmdlet. You use the <b>path<\/b> parameter to specify the original file, and the <b>newname<\/b> parameter for the new name. One thing that is important to keep in mind is that the <b>Rename-Item<\/b> cmdlet does not have the ability to move a file in the rename operation. If you need to move a file, use the <b>Move-Item<\/b> cmdlet. Because you cannot move a file with the <b>Rename-Item<\/b> cmdlet, the inclusion of the path in the <b>newname<\/b> parameter is redundant. The use of the parameter names themselves is also not required. You can also use the <b>ren<\/b> alias if you wish. The short version of the command is shown here:<\/p>\n<p><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\&gt;<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#0000ff\">ren<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\fso\\text1.txt<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">text1.bak<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\&gt;<\/font> <\/p>\n<p>The complete command with parameter names is shown here:<\/p>\n<p><font color=\"#0000ff\">Rename-Item<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\fso\\Text1.txt<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-NewName<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\fso\\text1.bak<\/font> <\/p>\n<p>\nAfter the backup copy of the file has been created, use the <b>Select-String<\/b> cmdlet to look for Word characters. This regular expression pattern is documented <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/az24scfc.aspx\">on MSDN<\/a>. The easiest way to open a text file and parse the contents is to use the <b>Select-String<\/b> cmdlet. We talked about using the <b>Select-String<\/b> cmdlet in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/07\/26\/parsing-an-auditpol-exe-report-with-windows-powershell-2-0.aspx\">yesterday\u2019s Hey, Scripting Guy! Blog<\/a>. The <b>Select-String<\/b> cmdlet can be used to search for a wildcard character pattern or for a regular expression pattern. As shown here, the <b>Select-String<\/b> cmdlet returns both the matching line of text, the file name, and the line number that contains the match:<\/p>\n<p><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\&gt;<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#0000ff\">Select-String<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-Pattern<\/font><font color=\"#808080\"> &quot;\\w&quot; <\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\fso\\Text1.bak<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">fso\\Text1.bak:<\/font><font color=\"#800000\">1<\/font><font color=\"#000000\">:This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">fso\\Text1.bak:<\/font><font color=\"#800000\">2<\/font><font color=\"#000000\">:This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">fso\\Text1.bak:<\/font><font color=\"#800000\">3<\/font><font color=\"#000000\">:This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">fso\\Text1.bak:<\/font><font color=\"#800000\">4<\/font><font color=\"#000000\">:This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">fso\\Text1.bak:<\/font><font color=\"#800000\">6<\/font><font color=\"#000000\">:This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">This<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">is<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">paragraph.<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">&lt;results<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">truncated&gt;<\/font> <\/p>\n<p>After the matching text is retrieved, use the <a href=\"http:\/\/www.bing.com\/visualsearch?g=powershell_cmdlets&amp;FORM=Z9GE22\">ForEach-Object cmdlet<\/a> to choose the matching line of text, and <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipe<\/a> the results to the new file. The complete command is shown here:<\/p>\n<p><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\&gt;<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#0000ff\">Select-String<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-Pattern<\/font><font color=\"#808080\"> &quot;\\w&quot; <\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\fso\\Text1.bak<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">|<\/font><font color=\"#808080\">&#160;<\/font>ForEach-Object<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#2b91af\">$_<\/font><font color=\"#000000\">.line<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#000000\">}<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">|<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#0000ff\">Set-Content<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-Path<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\fso\\Text1.txt<\/font><font color=\"#808080\">     <br \/><\/font><font color=\"#0000ff\">PS<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\&gt;<\/font> <\/p>\n<p>After the command has run, the newly created file will contain text only. This is shown in the following image<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1854.hsg072710041_61F56279.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Image of text file with extra returns and line feeds removed\" border=\"0\" alt=\"Image of text file with extra returns and line feeds removed\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6131.hsg072710041_thumb_6C4686D9.jpg\" width=\"604\" height=\"352\" \/><\/a> <\/p>\n<p>TB, that is all there is to using Windows PowerShell to remove extra returns and line feeds from a text file. String Week will continue tomorrow when we will talk about selecting specific columns of data from a tab-separated file. <\/p>\n<p>We would love you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&#160;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#160; Hey, Scripting Guy! Is it possible to use Windows PowerShell to groom a text file by deleting extra returns and line feeds it may contain? Here is the deal. I have a file that we need to download each day. This file contains updated prices for commodities used in our manufacturing operation. We use [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,21,45],"class_list":["post-17631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&#160; Hey, Scripting Guy! Is it possible to use Windows PowerShell to groom a text file by deleting extra returns and line feeds it may contain? Here is the deal. I have a file that we need to download each day. This file contains updated prices for commodities used in our manufacturing operation. We use [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17631"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17631\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}