{"id":4064,"date":"2021-05-28T13:54:12","date_gmt":"2021-05-28T18:54:12","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=4064"},"modified":"2021-05-28T13:56:29","modified_gmt":"2021-05-28T18:56:29","slug":"using-sed-to-format-a-phone-numer","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/using-sed-to-format-a-phone-numer\/","title":{"rendered":"Using sed to format a phone number"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/05\/image-6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"205\" src=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/05\/image-6-1024x205.png\" alt=\"\" class=\"wp-image-4065\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/05\/image-6-1024x205.png 1024w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/05\/image-6-300x60.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/05\/image-6-768x154.png 768w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/05\/image-6-500x100.png 500w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2021\/05\/image-6.png 1301w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>Formatting an unformated &#8220;phone&#8221; number using sed.<\/figcaption><\/figure>\n\n\n\n<p>There may be a different and easier way to do this, but the main thing to learn here is the ^, $, and [[:digit:]] options.<\/p>\n\n\n\n<p>^ refers to the first part of a line<br>&amp; which is our searched for pattern<br>$ refers to an end part of the line<br>[[:digit:]] searches for, you guessed it.  Digits!<\/p>\n\n\n\n<p>The following command reads the incoming 10 digit number form echo and does the following.<\/p>\n\n\n\n<p>the <strong>^<\/strong> tells it that the pattern needs to match at the beginning of the line<br><strong>[[:digit:]]<\/strong> repeated tells it to search for three consecutive digits<br><strong>(&amp;)<\/strong> tells it to put brackets around the &amp; which is our searched for pattern in the first part.<br>We then pipe that to another sed command which<br>searches for 4 consecutive digits<br>the <strong>$<\/strong> tells it that it needs to be at the end of the line.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo \"1234567890\" | sed -e 's\/<strong>^<\/strong>[[:digit:]][[:digit:]][[:digit:]]\/(&amp;) \/g' | sed -e 's\/[[:digit:]][[:digit:]][[:digit:]][[:digit:]]<strong>$<\/strong>\/-&amp;\/g'<\/pre>\n\n\n\n<p>Resulting output is<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(123) 456-7890<\/pre>\n\n\n\n<p>The following link was helpful while searching what the ^ and $ options do.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.computerhope.com\/unix\/used.htm\">https:\/\/www.computerhope.com\/unix\/used.htm<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There may be a different and easier way to do this, but the main thing to learn here is the ^, $, and [[:digit:]] options. ^ refers to the first part of a line&amp; which is our searched for pattern$ &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/using-sed-to-format-a-phone-numer\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[211,807,7,587,1224],"class_list":["post-4064","post","type-post","status-publish","format-standard","hentry","category-command-line","category-linux","tag-bash","tag-format","tag-linux-2","tag-sed","tag-tips-and-tricks"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=4064"}],"version-history":[{"count":3,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4064\/revisions"}],"predecessor-version":[{"id":4069,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4064\/revisions\/4069"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=4064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=4064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=4064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}