{"id":496,"date":"2020-11-21T17:11:26","date_gmt":"2020-11-21T17:11:26","guid":{"rendered":"https:\/\/blog.thomarite.uk\/?p=496"},"modified":"2020-11-21T17:15:16","modified_gmt":"2020-11-21T17:15:16","slug":"sed","status":"publish","type":"post","link":"https:\/\/blog.thomarite.uk\/index.php\/2020\/11\/21\/sed\/","title":{"rendered":"sed"},"content":{"rendered":"\n<p>This week I have to update a couple of yaml files and add a line in too many places. This is the chance to work smarter. So I searched how to add a line after a match and I found this <a href=\"https:\/\/fabianlee.org\/2018\/10\/28\/linux-using-sed-to-insert-lines-before-or-after-a-match\/\">link<\/a>. So let&#8217;s use <strong><em>sed<\/em><\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n$ cat test.yaml \nbgp:\n  bgp_as: 65000\n  enable: true\n  maximum_routes: 12000\n  neighbors:\n  - description: R1\n    route_map_in: RM-IN\n  - description: R2\n    route_map_in: RM-IN\n$ \n$ sed '\/route_map_in: RM-IN\/a route_map_out: RM-OUT' test.yaml\nbgp:\n  bgp_as: 65000\n  enable: true\n  maximum_routes: 12000\n  neighbors:\n  - description: R1\n    route_map_in: RM-IN\nroute_map_out: RM-OUT\n  - description: R2\n    route_map_in: RM-IN\nroute_map_out: RM-OUT\n<\/code><\/pre>\n\n\n\n<p>But the I needed to add some spaces to be aligned properly&#8230; So I searched again and found <a href=\"https:\/\/stackoverflow.com\/questions\/18439528\/sed-insert-line-with-spaces-to-a-specific-line\">this<\/a>. Now try again adding the spaces we need using &#8220;\\ &#8221; for each one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go:1.14.6|py:3.7.3|tomas@athens:~$ sed '\/route_map_in: RM-IN\/a \\ \\ \\ \\ route_map_out: RM-OUT' test.yaml\nbgp:\n  bgp_as: 65000\n  enable: true\n  maximum_routes: 12000\n  neighbors:\n  - description: R1\n    route_map_in: RM-IN\n    route_map_out: RM-OUT\n  - description: R2\n    route_map_in: RM-IN\n    route_map_out: RM-OUT<\/code><\/pre>\n\n\n\n<p>So now redirect to a new file to fully check and then you can rename:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sed '\/route_map_in: RM-IN\/a \\ \\ \\ \\ route_map_out: RM-OUT' test.yaml > test.yaml.new\n$ cat test.yaml.new \n$ mv test.yaml.new test.yaml<\/code><\/pre>\n\n\n\n<p>Now it is perfect aligned. It is kernel hack, but I am happy enough and saved a lot of time copy\/paste and errors for sure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week I have to update a couple of yaml files and add a line in too many places. This is the chance to work smarter. So I searched how to add a line after a match and I found this link. So let&#8217;s use sed: But the I needed to add some spaces to &hellip; <a href=\"https:\/\/blog.thomarite.uk\/index.php\/2020\/11\/21\/sed\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;sed&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-496","post","type-post","status-publish","format-standard","hentry","category-unix"],"_links":{"self":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/comments?post=496"}],"version-history":[{"count":3,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/496\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/496\/revisions\/499"}],"wp:attachment":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/media?parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/categories?post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/tags?post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}