{"id":73201,"date":"2015-09-22T00:01:00","date_gmt":"2015-09-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/22\/break-text-file-into-chunks-with-powershell\/"},"modified":"2019-02-18T09:35:09","modified_gmt":"2019-02-18T16:35:09","slug":"break-text-file-into-chunks-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/break-text-file-into-chunks-with-powershell\/","title":{"rendered":"Break Text File into Chunks with PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about breaking the contents of a text file into chunks with Windows PowerShell.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! The other day, you tweeted <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powertip-get-first-140-characters-from-string-with-powershell\/\" target=\"_blank\">Get First 140 Characters from String with PowerShell<\/a>. That is fine, but what about the rest of the text? I mean, if I want to break a text file into lines of 140 characters, how do I go about it? Can you provide a concrete example please?<\/p>\n<p>&mdash;BG<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BG,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The markets are mixed, there is a chance of afternoon thunderstorms, and there is a lizard scampering about on the outside patio. In short, it is a normal morning around the Scripting household. I am sitting here with a glass of cold Green Tea that the Scripting Wife made last night, and I am checking the email sent to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>. Indeed, it is a normal day. It is a cool 78 degrees Fahrenheit (26 degrees Celsius), and I am thinking that I will soon begin drinking more hot tea in the mornings. In fact, I have a friend in New York who promised to send me some green tea&hellip;cool.<\/p>\n<p>Anyway, BG, there are many ways of breaking a text file into pieces, but I will show you a really easy way to accomplish the task.<\/p>\n<h2>Read the contents of the file<\/h2>\n<p>The first thing I need to do is to read the contents of the text file. I will store the contents in a variable that I can use for later processing. The good thing is that Windows PowerShell makes it really easy to read the contents of a text file&mdash;I simply use the <b>Get-Content<\/b> cmdlet and specify the text file.<\/p>\n<p>The following text file is basically one really long line. I did not add any carriage returns or line feeds in the file. Here is what it looks like in Notepad:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-22-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-22-15-01.png\" alt=\"Image of text\" title=\"Image of text\" \/><\/a><\/p>\n<p>I then store the returned contents in a variable:<\/p>\n<p style=\"margin-left:30px\">$text = Get-Content -Path C:\\fso\\ATextFile.txt<\/p>\n<p>The next thing I do is initialize my position holder variable. I need to know where I am at in the file contents, so I use the <b>$i<\/b> variable to do this. Because I want to start at the beginning, I use 0 for the initial value. Later, I will increment the value by 140 each time I loop through the contents of the text. Here is the initialization:<\/p>\n<p style=\"margin-left:30px\">$i = 0<\/p>\n<h2>While means &quot;as long as&quot;<\/h2>\n<p>As long as the value of the <b>$i<\/b> variable is less than the length of the text file (minus 140), I want to keep reading from the text. To do this sort of &quot;as long as&quot; (&quot;something is true&quot; kind of thing), I use the <b>While<\/b> statement. The <b>While<\/b> statement consists of a condition that is evaluated and a script block that is executed.<\/p>\n<p>I create the condition to evaluate the position in the text, and then I use the script block to retrieve a chunk of 140 characters from my current position in the text. Then I increment the value of the <b>$i<\/b> variable by 140:<\/p>\n<p style=\"margin-left:30px\">While ($i -le ($text.length-140))<\/p>\n<p style=\"margin-left:30px\">&nbsp;{<\/p>\n<p style=\"margin-left:30px\">&nbsp; $text.Substring($i,140)<\/p>\n<p style=\"margin-left:30px\">&nbsp; $i += 140<\/p>\n<p style=\"margin-left:30px\">&nbsp;}<\/p>\n<h2>What about leftovers?<\/h2>\n<p>It should be pretty obvious that I would be extremely lucky if my text came out to be an exact multiple of 140 characters. (What would that be? One chance in a hundred and forty?) Anyway, what I need to do is take the position of <b>$i<\/b> and display the remaining text. If I do not specify the second number (that is the amount of text to retrieve), <b>SubString<\/b> will begin at the position and return everything. So in this way, I can get the remainder text. This is shown here:<\/p>\n<p style=\"margin-left:30px\">$text.Substring($i)<\/p>\n<p style=\"margin-left:30px\">Here is the complete script:<\/p>\n<p style=\"margin-left:30px\">$text = Get-Content -Path C:\\fso\\ATextFile.txt<\/p>\n<p style=\"margin-left:30px\">$i = 0<\/p>\n<p style=\"margin-left:30px\">While ($i -le ($text.length-140))<\/p>\n<p style=\"margin-left:30px\">&nbsp;{<\/p>\n<p style=\"margin-left:30px\">&nbsp; $text.Substring($i,140)<\/p>\n<p style=\"margin-left:30px\">&nbsp; $i += 140<\/p>\n<p style=\"margin-left:30px\">&nbsp;}<\/p>\n<p style=\"margin-left:30px\">$text.Substring($i)<\/p>\n<p>The script and the output from the script are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-22-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-22-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>BG, that is all there is to using Windows PowerShell to break a text file into chunks. Join me tomorrow when I will talk about more way cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about breaking the contents of a text file into chunks with Windows PowerShell. &nbsp;Hey, Scripting Guy! The other day, you tweeted Get First 140 Characters from String with PowerShell. That is fine, but what about the rest of the text? I mean, if I want to break a [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,617,45],"class_list":["post-73201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about breaking the contents of a text file into chunks with Windows PowerShell. &nbsp;Hey, Scripting Guy! The other day, you tweeted Get First 140 Characters from String with PowerShell. That is fine, but what about the rest of the text? I mean, if I want to break a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73201","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=73201"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73201\/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=73201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}