{"id":15161,"date":"2011-03-23T00:01:00","date_gmt":"2011-03-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/03\/23\/use-powershell-to-look-for-phone-numbers-in-text\/"},"modified":"2011-03-23T00:01:00","modified_gmt":"2011-03-23T00:01:00","slug":"use-powershell-to-look-for-phone-numbers-in-text","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-look-for-phone-numbers-in-text\/","title":{"rendered":"Use PowerShell to Look for Phone Numbers in Text"},"content":{"rendered":"<p><b>Summary<\/b>: The Scripting Wife learns how to use a regular expression pattern to look for phone numbers in text files.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, here. I am looking over some of the slide decks from the Columbia, South Carolina SQL Saturday #70 event. There were a couple of really good Windows PowerShell sessions during the day (including the one that I presented on Windows PowerShell Best Practices for database administrators (DBAs). <\/p>\n<p>I am midway through Brian K. McDonald&rsquo;s SQL Reporting presentation deck, and I sense the presence of another person in my office. I am careful not to move my head, but I look out of the corner of my eye. Sure enough, it is the Scripting Wife (but then, we are the only two in the house&mdash;but it could have been a poltergeist). I decide I will wait to see what she says. Silence begins to fill the room like pine needles covering a fresh cut lawn. Finally, I break down.<\/p>\n<p>&ldquo;Hello. What are you up to?&rdquo; I ask.<\/p>\n<p>&ldquo;You know how yesterday you showed me how to pick out specific lines in a text file. I thought that would work for me, but I guess I mixed up some of the lines in my text files. I need a better way to find things like the telephone numbers in my files,&rdquo; she said.<\/p>\n<p>&ldquo;I have a great way to do that very thing. But it will involve extending our lesson on regular expressions from <a target=\"_blank\" href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/scripting-wife-uses-regular-expressions-to-match-a-string\/\">Sunday<\/a>,&rdquo; I said.<\/p>\n<p>&ldquo;What do you mean extend our lesson?&rdquo; she asked.<\/p>\n<p>&ldquo;Well on Sunday, I taught you the caret (<b>^<\/b>) special character, which tells the regular expression to begin matching at the beginning of the line. The best way to match a telephone number is to use another special character&mdash; the <b>backslash d<\/b>,&rdquo; I said. <\/p>\n<p>&ldquo;I see,&rdquo; she said with a bit of reluctance.<\/p>\n<p>&ldquo;It is easy. Create a variable named <b>a<\/b>, and then assign a bogus telephone number to it. Then use the <b>match<\/b> operator to match <b>caret backslash d<\/b>,&rdquo; I instructed.<\/p>\n<p>The Scripting Wife thought for a minute, and typed the following commands (&lt;space&gt; is the Space bar, &lt;enter&gt; is the Enter key or Return key).<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$a&lt;space&gt;=&lt;space&gt;&#8221;123-123-1234&#8243;&lt;enter&gt;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$a&lt;space&gt;-match&lt;space&gt;&#8217;^\\d'&lt;enter&gt;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The following is her command and the associated output.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1016.HSg-3-23-11-1_52813FD5.jpg\"><img decoding=\"async\" height=\"106\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2818.HSg-3-23-11-1_thumb_563EE4A5.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;So you see, my dear Scripting Wife, that regular expression pattern finds a match with the bogus phone number. Use <b>Select-String<\/b> to see if it finds Lori&rsquo;s phone number,&rdquo; I said.<\/p>\n<p>&ldquo;OK,&rdquo; she said.<\/p>\n<p>The Scripting Wife thought for a second or two, and then cautiously typed the following command.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Select-String -Path C:\\MyFriends\\Lori.txt -Pattern &#8216;^\\d&#8217;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The command and its associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8272.hsg-3-23-11-2_7EFA49EE.jpg\"><img decoding=\"async\" height=\"106\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8765.hsg-3-23-11-2_thumb_2A5E6AE9.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;Well that almost worked,&rdquo; she said. &ldquo;Why do I also have Lori&rsquo;s street address?&rdquo;<\/p>\n<p>&ldquo;That is because the <b>caret backslash d<\/b> regular expression matches any line that begins with a number. In your Lori.txt file there are two lines that begin with numbers,&rdquo; I said. The <a>Lori.txt file <\/a>is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5732.hsg-3-23-11-3_5960A6C0.jpg\"><img decoding=\"async\" height=\"238\" width=\"304\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8875.hsg-3-23-11-3_thumb_761A1BD5.jpg\" alt=\"Image of contact information\" border=\"0\" title=\"Image of contact information\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;What we need to do now is modify the regular expression pattern so that it will only match three numbers, add a dash and three more numbers, and then add another dash followed by four numbers. To do this, you need to learn how to say three numbers. You put the number 3 inside a set of curly brackets. Go ahead and try that,&rdquo; I said.<\/p>\n<p>The Scripting Wife thought for a little bit, used the Up arrow to retrieve her previous command, and modified it with <b>{3}<\/b>. She also added the dashes to separate the different number groups. The command she created appears here.<\/p>\n<blockquote>\n<p><span style=\"line-height: normal;list-style-type: disc;font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Select-String -Path C:\\MyFriends\\Lori.txt -Pattern &#8216;^\\d{3}-\\d{3}-\\d{4}&#8217;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>When she ran the command, the output shown here appeared.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4643.HSG-3-23-11-4_4F6B8F88.jpg\"><img decoding=\"async\" height=\"194\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1004.HSG-3-23-11-4_thumb_2CC7510D.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;Wow, did I do that?&rdquo; the Scripting Wife said, amazed at her progress.<\/p>\n<p>&ldquo;Indeed you did,&rdquo; I said, somewhat pleased with both myself and with her.<\/p>\n<p>&ldquo;It looks really complicated, but it is actually pretty simple in small pieces,&rdquo; she said. &ldquo;Thank you.&rdquo;<\/p>\n<p>And with that she was gone. Who knows where she was headed&hellip; <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The Scripting Wife learns how to use a regular expression pattern to look for phone numbers in text files. Microsoft Scripting Guy, Ed Wilson, here. I am looking over some of the slide decks from the Columbia, South Carolina SQL Saturday #70 event. There were a couple of really good Windows PowerShell sessions during [&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":[38,51,174,3,4,74,45],"class_list":["post-15161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-getting-started","tag-regular-expressions","tag-scripting-guy","tag-scripting-techniques","tag-scripting-wife","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: The Scripting Wife learns how to use a regular expression pattern to look for phone numbers in text files. Microsoft Scripting Guy, Ed Wilson, here. I am looking over some of the slide decks from the Columbia, South Carolina SQL Saturday #70 event. There were a couple of really good Windows PowerShell sessions during [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15161","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=15161"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15161\/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=15161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}