A Visual Guide to Sed
<p>While <code><a href="https://github.com/chmln/sd" rel="noopener ugc nofollow" target="_blank">sd</a></code> is a better alternative these days, learning <code>sed</code> has some advantages:</p>
<ul>
<li>It’s a standard UNIX tool, available everywhere.</li>
<li>It shares syntax with <code>vim</code>’s command-mode.</li>
<li><code>sed</code> is based.</li>
</ul>
<p>However, mastering <code>sed</code> takes some time — <code>sed</code> is actually a non-interactive text editor with a built-in Turing machine (you can even run <a href="https://github.com/uuner/sedtris" rel="noopener ugc nofollow" target="_blank">Tetris</a> 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 <code>sed SCRIPT FILE</code>. For instance, to replace all occurrences of <code>foo</code> with <code>bar</code> in <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 <code>-i/--in-place</code> 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, <code>sed</code> prints all processed input, reflecting any modifications made. You can suppress this output with the <code>-n/--quiet/--silent</code> option. In that case, you need to run specific commands to get any output at all, like the <code>p</code> 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>''</code>) when using <code>sed</code> in a shell.</p>
<p><a href="https://betterprogramming.pub/a-visual-guide-to-sed-a7a8abd2f675">Read More</a></p>