{"id":65713,"date":"2007-01-17T06:40:00","date_gmt":"2007-01-17T06:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/01\/17\/how-can-i-delete-just-the-ip-addresses-from-each-line-in-a-text-file\/"},"modified":"2007-01-17T06:40:00","modified_gmt":"2007-01-17T06:40:00","slug":"how-can-i-delete-just-the-ip-addresses-from-each-line-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-just-the-ip-addresses-from-each-line-in-a-text-file\/","title":{"rendered":"How Can I Delete Just the IP Addresses From Each Line in a Text File?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" 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\"> <\/H2>\n<P>Hey, Scripting Guy! I have a file that has IP addresses and server names on each line. How can I delete the IP addresses, leaving just the server names?<BR><BR>&#8212; RR<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" 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\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RR. In case you\u2019re wondering, as we\u2019re writing today\u2019s column temperatures are still right around freezing and there\u2019s still snow and ice everywhere you look. Admittedly, many of you aren\u2019t feeling particularly sorry for the Scripting Guys. \u201cIt\u2019s <I>33 degrees<\/I> and you\u2019re saying it\u2019s too cold?!? Holy smokes, here in [<I>insert place name<\/I>] it\u2019s <I>3<\/I> degrees and you don\u2019t hear anyone whining and crying about the weather! Is everyone in Seattle a wimp?\u201d<\/P>\n<P>Well, here\u2019s what we have to say about <I>that<\/I>: yes, yes we are. Each and every one of us.<\/P>\n<P>In fact, not only are we all wimps, but some of us are downright weird as well. For example, the other morning the Scripting Guy who writes this column arrived at the bus stop to find several people dutifully waiting for the good old 245. One guy waiting was wearing a heavy sweatshirt with the hood pulled up over his stocking cap. He was also wearing tennis shoes with no socks and a pair of running shorts. As he stood there, holding a pair of gloves in his hands (no, not <I>on<\/I> his hands, <I>in<\/I> his hands) he said to no one in particular, \u201cSure is cold this morning, isn\u2019t it?\u201d <\/P>\n<P>Yeah, sure is, isn\u2019t it?<\/P>\n<P>So how do weather wimps like us get through these gloomy winter days? Well we can\u2019t speak for any of our fellow Seattleites, but the Scripting Guys spend <I>their<\/I> time writing scripts that can delete just the IP addresses from a file, leaving behind nothing but server names:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)<\/p>\n<p>Do Until objFile.AtEndOfStream\n    strLine = objFile.ReadLine\n    arrItems = Split(strLine, &#8220;, &#8220;)\n    strNewContent = strNewContent &amp; arrItems(1) &amp; vbCrLf\nLoop<\/p>\n<p>objFile.Close<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.Write strNewContent\nobjFile.Close\n<\/PRE>\n<P>Ah, yes: there\u2019s nothing like a script to make you feel all warm and toasty, is there? We\u2019ll explain how this script works in just a second. Before we do that, however, we should note that we\u2019re assuming that the text file in question looks something like this:<\/P><PRE class=\"codeSample\">192.168.1.1, atl-dc-01\n192.168.1.2, atl-dc-02\n192.168.1.3, atl-dc-03\n192.168.1.4, atl-dc-04\n<\/PRE>\n<P>What we <I>want<\/I> it to look like is this:<\/P><PRE class=\"codeSample\">atl-dc-01\natl-dc-02\natl-dc-03\natl-dc-04\n<\/PRE>\n<P>How are we going to do that? Here\u2019s how.<\/P>\n<P>To begin with, we define a pair of constants, ForReading (with a value of 1) and ForWriting (with a value of 2); we\u2019ll use these constants when we open our text file. And yes, we need two constants because we\u2019ll actually have to open the text file twice: once to read in the existing contents, and once to save the revised contents. That\u2019s just the way you have to do things when working with text files.<\/P>\n<P>After defining the constants we then use these two lines of code to create an instance of the <B>Scripting.FileSystemObject<\/B> and to open the file C:\\Scripts\\Test.txt for reading:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\n<\/PRE>\n<P>And now the fun begins.<\/P>\n<P>For starters, we create a Do Until loop that runs until we\u2019ve reached the end of the text file (or, as we Seattleites like to say, until the file\u2019s <B>AtEndOfStream<\/B> property is True). Inside the loop we use the <B>ReadLine<\/B> method to read the first line of the text file and store it in a variable named strLine. That means that, the first time through the loop, strLine will be equal to this:<\/P><PRE class=\"codeSample\">192.168.1.1, atl-dc-01\n<\/PRE>\n<P>That\u2019s all well and good, of course, except for one thing: that\u2019s not what we <I>want<\/I> strLine to be equal to. Instead, we want it to be equal to this:<\/P><PRE class=\"codeSample\">atl-dc-01\n<\/PRE>\n<P>Consequently, in our next line of code we use the <B>Split<\/B> method to create an array (named arrItems), designating a comma followed by a blank space (<B>, <\/B>) as the delimiter we want to split on. What will that do for us? That will give us an array consisting of these two items:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>192.168.1.1<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>atl-dc-01<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>As you can see, the second item (which has an index number of 1; remember, the first item in an array always has an index number of 0) just happens to be the name of the computer, the value we want saved in our text file. Because of that we use this line of code to grab the value of item 1 and store it in a variable named strNewContent:<\/P><PRE class=\"codeSample\">strNewContent = strNewContent &amp; arrItems(1) &amp; vbCrLf\n<\/PRE>\n<P>OK, you\u2019re right: we\u2019re actually storing more than just the value of that array item in strNewContent. Instead, we\u2019re assigning strNewContent the existing value of strNewContent <I>plus<\/I> array item 1 <I>plus<\/I> a carriage return-linefeed (vbCrLf). Of course, the first time through the loop strNewContent won\u2019t <I>have<\/I> a value; therefore, strNewContent will end up being equal to this:<\/P><PRE class=\"codeSample\">atl-dc-01\n<\/PRE>\n<P>We then loop around and repeat the process with the second line in the text file. That results in arrItems(1) being equal to atl-dc-02 and, when all is said and done, makes strNewContent equal to this:<\/P><PRE class=\"codeSample\">atl-dc-01\natl-dc-02\n<\/PRE>\n<P>And then we simply loop around and start all over again, repeating the process until we\u2019ve read and modified each and every line in the text file. When we\u2019re done strNewContent will be equal to this:<\/P><PRE class=\"codeSample\">atl-dc-01\natl-dc-02\natl-dc-03\natl-dc-04\n<\/PRE>\n<P>Which, remarkably enough, is exactly what we want our text file to look like.<\/P>\n<P>The rest is easy. We close the file Test.txt and then immediately reopen it, this time for writing (using the constant ForWriting). We use the <B>WriteLine<\/B> method to write the value of strNewContent to the file, then close the file for the last time. At that point we can go back to whining and complaining about the weather.<\/P>\n<P>Incidentally, as an alumnus of the University of Washington the Scripting Guy who writes this column feels he should add one final note. The crazy guy at the bus stop, the one who was wearing running shorts and no socks and yet was complaining about it being cold? His sweatshirt was emblazoned <I>Washington State University<\/I>. No further comment needed, if you know what we mean.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a file that has IP addresses and server names on each line. How can I delete the IP addresses, leaving just the server names?&#8212; RR Hey, RR. In case you\u2019re wondering, as we\u2019re writing today\u2019s column temperatures are still right around freezing and there\u2019s still snow and ice everywhere you [&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,14,5],"class_list":["post-65713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a file that has IP addresses and server names on each line. How can I delete the IP addresses, leaving just the server names?&#8212; RR Hey, RR. In case you\u2019re wondering, as we\u2019re writing today\u2019s column temperatures are still right around freezing and there\u2019s still snow and ice everywhere you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65713","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=65713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65713\/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=65713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}