{"id":7462,"date":"2021-02-11T22:32:51","date_gmt":"2021-02-11T21:32:51","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=7462"},"modified":"2021-02-12T09:43:14","modified_gmt":"2021-02-12T08:43:14","slug":"sed-editing-text-files","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/sed-editing-text-files\/","title":{"rendered":"sed: Editing text files"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_7462 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_7462')){$('.twoclick_social_bookmarks_post_7462').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"sed%3A%20Editing%20text%20files\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Ahere%20some%20often%20requested%20basic%20examples%20on%20how%20to%20edit%20text%20files%20with%20sed.%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/sed-editing-text-files\\\/\",\"post_id\":7462,\"post_title_referrer_track\":\"sed%3A+Editing+text+files\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>here some often requested basic examples on how to edit text files with <strong>sed<\/strong>.<br \/>\n<!--more--><br \/>\nAn example file, here a NetworkManager interface config, filename config.net:<br \/>\n<code>[connection]<br \/>\nid=int_with_static_routes<br \/>\nuuid=75241683-27b0-125e-666a-b58823c177d14d<br \/>\ntype=ethernet<br \/>\nautoconnect-priority=-999<br \/>\ninterface-name=eth0<br \/>\npermissions=<br \/>\ntimestamp=1534541432<\/p>\n<p>[ethernet]<br \/>\nmac-address-blacklist=<\/p>\n<p>[ipv4]<br \/>\ndns-search=<br \/>\nmethod=auto<br \/>\nroute1=192.168.192.234\/32,10.10.254.10,1<br \/>\nroute2=192.168.201.1\/32,10.10.254.10,1<br \/>\n<\/code><\/p>\n<p>By default sed writes to stdout, to console. If you want to edit the file directly append &#8211;in-place=.bak. This also creates a backup file of the file before editing.<\/p>\n<p>Replace the first match (first match means first match in line). sed proceeds line by line.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsed -e's\/192\/172\/' config.txt\r\n<\/pre>\n<p><code><br \/>\n...<br \/>\nmethod=auto<br \/>\nroute1=<strong>172<\/strong>.168.192.234\/32,10.10.254.10,1<br \/>\nroute2=<strong>172<\/strong>.168.201.1\/32,10.10.254.10,1<br \/>\n<\/code><br \/>\nReplace all matches in a line<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsed -e's\/192\/172\/g' config.txt\r\n<\/pre>\n<p><code><br \/>\nmethod=auto<br \/>\nroute1=<strong>172<\/strong>.168.<strong>172<\/strong>.234\/32,10.10.254.10,1<br \/>\nroute2=<strong>172<\/strong>.168.201.1\/32,10.10.254.10,1<br \/>\n<\/code><\/p>\n<p>Append a line after a match => a switch<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsed -e'\/route1=192.168.192.234\\\/32,10.10.254.10,1\/a route2=192.168.230.34\\\/32,10.113.56.148,1' config.txt\r\n<\/pre>\n<p><code><br \/>\n...method=auto<br \/>\nroute1=192.168.192.234\/32,10.10.254.10,1<br \/>\n<strong>route2=192.168.230.34\/32,10.113.56.148,1<\/strong><br \/>\nroute2=192.168.201.1\/32,10.10.254.10,1<br \/>\n<\/code><br \/>\nAdd a line before a match => i switch<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsed -e'\/route1=192.168.192.234\\\/32,10.10.254.10,1\/i route0=192.168.230.34\\\/32,10.113.56.148,1' config.txt\r\n<\/pre>\n<p><code><br \/>\n...<br \/>\nmethod=auto<br \/>\nroute0=192.168.230.34\/32,10.113.56.148,1<br \/>\nroute1=192.168.192.234\/32,10.10.254.10,1<br \/>\nroute2=192.168.201.1\/32,10.10.254.10,1<br \/>\n<\/code><\/p>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, here some often requested basic examples on how to edit text files with sed.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1532,1533,1531,522,1530],"class_list":["post-7462","post","type-post","status-publish","format-standard","hentry","category-linux","tag-append","tag-before","tag-insert","tag-replace","tag-sed"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/7462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=7462"}],"version-history":[{"count":12,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/7462\/revisions"}],"predecessor-version":[{"id":7474,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/7462\/revisions\/7474"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=7462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=7462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=7462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}