{"id":516,"date":"2019-06-01T06:59:16","date_gmt":"2019-06-01T06:59:16","guid":{"rendered":"https:\/\/www.24x7serversupport.com\/24x7serversupport-blog\/?p=516"},"modified":"2019-06-01T06:59:16","modified_gmt":"2019-06-01T06:59:16","slug":"ways-to-use-sed-command","status":"publish","type":"post","link":"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/","title":{"rendered":"Ways to use sed command"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">View selected lines of a document\/file<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed -n &#8216;9,20p&#8217; file.txt<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">View the entire file except selected lines<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed &#8216;21,40d&#8217; file.txt<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Replacing words or characters in a file<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed &#8216;s\/centos\/ubuntu\/g&#8217; file.txt<\/p><\/blockquote>\n\n\n\n<p>To make changes permanent, you can use option &#8216;-i&#8217;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed -i &#8216;s\/centos\/ubuntu\/g&#8217; file.txt<\/p><\/blockquote>\n\n\n\n<p> flag \/g (global replacement) <\/p>\n\n\n\n<p>Use the combination of \/1, \/2 etc and \/g to replace all the patterns from the <em>nth <\/em>occurrence of a pattern in the file.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed &#8216;s\/centos\/ubuntu\/4g&#8217; file.txt <\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Using regular expressions<\/h3>\n\n\n\n<p>To remove empty lines or those beginning with\u00a0<code>#<\/code>\u00a0from the Apache configuration file, do:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed &#8216;\/^#|^$| *#\/d&#8217; my.cnf<\/p><\/blockquote>\n\n\n\n<p>The caret sign followed by the number sign&nbsp;<code>(^#)<\/code>&nbsp;indicates the beginning of a line, whereas&nbsp;<code>^$<\/code>&nbsp;represents blank lines. The vertical bars indicate boolean operations, whereas the backward slash is used to escape the vertical bars.<\/p>\n\n\n\n<p>In this particular case, the Apache configuration file has lines with\u00a0<code>#\u2019s<\/code>\u00a0not at the beginning of some lines, so\u00a0<code>*#<\/code>\u00a0is used to remove those as well.<\/p>\n\n\n\n<p>To replace a word beginning with uppercase or lowercase with another word, we can also use sed. To illustrate, let\u2019s replace the word\u00a0<strong>zip<\/strong>\u00a0or\u00a0<strong>Zip<\/strong>\u00a0with rar in <strong>file.txt<\/strong>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed &#8216;s\/[Zz]ip\/tar.gz\/g&#8217; file.txt<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Performing multiple substitutions in a single command<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># sed -i &#8216;s\/ubuntu\/centos\/gi;s\/plesk\/cpanel\/gi&#8217; file.txt<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Combine sed with other commands<\/h3>\n\n\n\n<p>Firstly, replace multiple blank spaces with a single space, we will use the output of\u00a0<code>ip route show<\/code>\u00a0and a pipeline<\/p>\n\n\n\n<p>Print the lines beginning with src. <br \/>Then convert multiple spaces into a single one. Finally, cut the\u00a0<strong>9th<\/strong>\u00a0field (considering a single space as field separator), which is where the IP address is: <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p># ip route show | sed -n &#8216;\/src\/p&#8217; | sed -e &#8216;s\/ *\/ \/g&#8217; | cut -d&#8217; &#8216; -f9<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>View selected lines of a document\/file # sed -n &#8216;9,20p&#8217; file.txt View the entire file except selected lines # sed &#8216;21,40d&#8217; file.txt Replacing words or characters in a file # sed &#8216;s\/centos\/ubuntu\/g&#8217; file.txt To make changes permanent, you can use option &#8216;-i&#8217; # sed -i &#8216;s\/centos\/ubuntu\/g&#8217; file.txt flag \/g (global replacement) Use the combination of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[30,34],"tags":[124,112,125,126,127],"class_list":["post-516","post","type-post","status-publish","format-standard","hentry","category-centos","category-linux","tag-command","tag-linux","tag-sed","tag-send-command","tag-unix"],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ways to use sed command | 24x7serversupport Blog<\/title>\n<meta name=\"description\" content=\"Ways to use sed command on linux and unix systems\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ways to use sed command | 24x7serversupport Blog\" \/>\n<meta property=\"og:description\" content=\"Ways to use sed command on linux and unix systems\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/\" \/>\n<meta property=\"og:site_name\" content=\"24x7serversupport Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-01T06:59:16+00:00\" \/>\n<meta name=\"author\" content=\"24x7support\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@24x7serversuppo\" \/>\n<meta name=\"twitter:site\" content=\"@24x7serversuppo\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"24x7support\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/\",\"url\":\"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/\",\"name\":\"Ways to use sed command | 24x7serversupport Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#website\"},\"datePublished\":\"2019-06-01T06:59:16+00:00\",\"dateModified\":\"2019-06-01T06:59:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401\"},\"description\":\"Ways to use sed command on linux and unix systems\",\"breadcrumb\":{\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.24x7serversupport.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ways to use sed command\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#website\",\"url\":\"https:\/\/www.24x7serversupport.com\/blog\/\",\"name\":\"24x7serversupport Blog\",\"description\":\"Linux | CPanel | WHM | webhosting| Plesk | DirectAdmin | CentOs | Debian | Ubuntu Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.24x7serversupport.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401\",\"name\":\"24x7support\",\"url\":\"https:\/\/www.24x7serversupport.com\/blog\/author\/24x7support\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ways to use sed command | 24x7serversupport Blog","description":"Ways to use sed command on linux and unix systems","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/","og_locale":"en_US","og_type":"article","og_title":"Ways to use sed command | 24x7serversupport Blog","og_description":"Ways to use sed command on linux and unix systems","og_url":"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/","og_site_name":"24x7serversupport Blog","article_published_time":"2019-06-01T06:59:16+00:00","author":"24x7support","twitter_card":"summary_large_image","twitter_creator":"@24x7serversuppo","twitter_site":"@24x7serversuppo","twitter_misc":{"Written by":"24x7support","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/","url":"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/","name":"Ways to use sed command | 24x7serversupport Blog","isPartOf":{"@id":"https:\/\/www.24x7serversupport.com\/blog\/#website"},"datePublished":"2019-06-01T06:59:16+00:00","dateModified":"2019-06-01T06:59:16+00:00","author":{"@id":"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401"},"description":"Ways to use sed command on linux and unix systems","breadcrumb":{"@id":"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.24x7serversupport.com\/blog\/ways-to-use-sed-command\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.24x7serversupport.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Ways to use sed command"}]},{"@type":"WebSite","@id":"https:\/\/www.24x7serversupport.com\/blog\/#website","url":"https:\/\/www.24x7serversupport.com\/blog\/","name":"24x7serversupport Blog","description":"Linux | CPanel | WHM | webhosting| Plesk | DirectAdmin | CentOs | Debian | Ubuntu Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.24x7serversupport.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.24x7serversupport.com\/blog\/#\/schema\/person\/decfb5fad6bde6ac6822d4e965c6d401","name":"24x7support","url":"https:\/\/www.24x7serversupport.com\/blog\/author\/24x7support\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/posts\/516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/comments?post=516"}],"version-history":[{"count":0,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/posts\/516\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/media?parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/categories?post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.24x7serversupport.com\/blog\/wp-json\/wp\/v2\/tags?post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}