A Visual Guide to Sed

<p>While&nbsp;<code><a href="https://github.com/chmln/sd" rel="noopener ugc nofollow" target="_blank">sd</a></code>&nbsp;is a better alternative these days, learning&nbsp;<code>sed</code>&nbsp;has some advantages:</p> <ul> <li>It&rsquo;s a standard UNIX tool, available everywhere.</li> <li>It shares syntax with&nbsp;<code>vim</code>&rsquo;s command-mode.</li> <li><code>sed</code>&nbsp;is based.</li> </ul> <p>However, mastering&nbsp;<code>sed</code>&nbsp;takes some time &mdash;&nbsp;<code>sed</code>&nbsp;is actually a non-interactive text editor with a built-in Turing machine (you can even run&nbsp;<a href="https://github.com/uuner/sedtris" rel="noopener ugc nofollow" target="_blank">Tetris</a>&nbsp;on it).</p> <p>This guide will show you how it works.</p> <h2>Overview</h2> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/1*-Ecme2X-i2qeMjgkJV1gYw.png" style="height:124px; width:700px" /></p> <p>sed invocation syntax.</p> <p>The invocation is typically&nbsp;<code>sed SCRIPT FILE</code>. For instance, to replace all occurrences of&nbsp;<code>foo</code>&nbsp;with&nbsp;<code>bar</code>&nbsp;in&nbsp;<code>input.txt</code>, you would use:</p> <p>Sometimes you may want to edit the original file rather than create a new one. In that case, use the&nbsp;<code>-i/--in-place</code>&nbsp;option.</p> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/1*G2to5bTJ0MdDMLjUtJ3y4w.png" style="height:131px; width:700px" /></p> <p>In-place editing.</p> <p>By default,&nbsp;<code>sed</code>&nbsp;prints all processed input, reflecting any modifications made. You can suppress this output with the&nbsp;<code>-n/--quiet/--silent</code>&nbsp;option. In that case, you need to run specific commands to get any output at all, like the&nbsp;<code>p</code>&nbsp;command:</p> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/1*D0YS89McQaq6ModUbL49dQ.png" style="height:203px; width:700px" /></p> <p>This command prints only line 45.</p> <p>Remember to quote the script (<code>&#39;&#39;</code>) when using&nbsp;<code>sed</code>&nbsp;in a shell.</p> <p><a href="https://betterprogramming.pub/a-visual-guide-to-sed-a7a8abd2f675">Read More</a></p>