|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
|
6 | 6 | <link href="https://learnbyexample.github.io"/>
|
7 | 7 | <generator uri="https://www.getzola.org/">Zola</generator>
|
8 |
| - <updated>2023-10-12T00:00:00+00:00</updated> |
| 8 | + <updated>2023-10-17T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>CLI text editing with ed</title> |
| 12 | + <published>2023-10-17T00:00:00+00:00</published> |
| 13 | + <updated>2023-10-17T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/mini/cli-text-editing-with-ed/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/mini/cli-text-editing-with-ed/</id> |
| 16 | + <content type="html"><p>I'm finally writing a post on the <code>ed</code> command. And I'm keeping it short so that I'll actually publish the post. The examples presented below will be easier to understand for those already familiar with Vim and <code>sed</code>. See the links at the end for learning resources.</p> |
| 17 | +<p>Although I'm interested in getting to know <code>ed</code> better, I don't really find myself in situations where it'd help me. But, I have used it a few times to answer questions on stackoverflow.</p> |
| 18 | +<h2 id="moving-lines">Moving lines</h2> |
| 19 | +<p>Consider this sample input file:</p> |
| 20 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ cat ip.txt |
| 21 | +</span><span>apple |
| 22 | +</span><span>banana |
| 23 | +</span><span>cherry |
| 24 | +</span><span>fig |
| 25 | +</span><span>mango |
| 26 | +</span><span>pineapple |
| 27 | +</span></code></pre> |
| 28 | +<p>Suppose, you want to move the third line to the top. If you are using Vim, you can execute <code>:3m0</code> where <code>3</code> is the input address, <code>m</code> is the <em>move</em> command and <code>0</code> is the target address. To do the same with <code>ed</code>:</p> |
| 29 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39;3m0\nwq\n&#39; </span><span style="color:#72ab00;">|</span><span> ed </span><span style="color:#72ab00;">-</span><span>s ip.txt </span><span style="color:#72ab00;">- |
| 30 | +</span><span> |
| 31 | +</span><span>$ cat ip.txt |
| 32 | +</span><span>cherry |
| 33 | +</span><span>apple |
| 34 | +</span><span>banana |
| 35 | +</span><span>fig |
| 36 | +</span><span>mango |
| 37 | +</span><span>pineapple |
| 38 | +</span></code></pre> |
| 39 | +<p>The <code>3m0</code> part in the above <code>ed</code> command is identical to the Vim solution. After that, another command <code>wq</code> (write and quit) is issued to save the changes (again, Vim users would be familiar with this combination). The <code>-s</code> option suppresses diagnostics and other details. <code>-</code> is used to indicate that the <code>ed</code> script is passed via stdin.</p> |
| 40 | +<p>You can also move lines based on a regexp match. Here's an example:</p> |
| 41 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span style="color:#7f8989;"># move the first matching line containing &#39;an&#39; to the top of the file |
| 42 | +</span><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39;/an/m0\nwq\n&#39; </span><span style="color:#72ab00;">|</span><span> ed </span><span style="color:#72ab00;">-</span><span>s ip.txt </span><span style="color:#72ab00;">- |
| 43 | +</span><span> |
| 44 | +</span><span>$ cat ip.txt |
| 45 | +</span><span>banana |
| 46 | +</span><span>cherry |
| 47 | +</span><span>apple |
| 48 | +</span><span>fig |
| 49 | +</span><span>mango |
| 50 | +</span><span>pineapple |
| 51 | +</span></code></pre> |
| 52 | +<p>If you want to move all the matching lines, you can use the <code>g</code> command (same as Vim). Note that the first matching line will be moved first, then the next matching line and so on. So the order will be reversed after the move.</p> |
| 53 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39;g/app/m0\nwq\n&#39; </span><span style="color:#72ab00;">|</span><span> ed </span><span style="color:#72ab00;">-</span><span>s ip.txt </span><span style="color:#72ab00;">- |
| 54 | +</span><span> |
| 55 | +</span><span>$ cat ip.txt |
| 56 | +</span><span>pineapple |
| 57 | +</span><span>apple |
| 58 | +</span><span>banana |
| 59 | +</span><span>cherry |
| 60 | +</span><span>fig |
| 61 | +</span><span>mango |
| 62 | +</span></code></pre> |
| 63 | +<p>Here's the <a href="https://stackoverflow.com/q/67031062/4082052">stackoverflow link</a> that inspired the above examples. See <a href="https://stackoverflow.com/a/75222984/4082052">this stackoverflow answer</a> for more examples of moving lines. See <a href="https://stackoverflow.com/a/48840851/4082052">this one</a> to learn how to copy a particular line to the end of the file.</p> |
| 64 | +<h2 id="negative-addressing">Negative addressing</h2> |
| 65 | +<p>There are plenty of <a href="https://learnbyexample.github.io/learn_gnused/selective-editing.html">addressing features</a> provided by the <code>GNU sed</code> command, but negative addressing isn't one. Here's an example of deleting the last but second line using <code>ed</code>:</p> |
| 66 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ cat colors.txt |
| 67 | +</span><span>red |
| 68 | +</span><span>green |
| 69 | +</span><span>blue |
| 70 | +</span><span>yellow |
| 71 | +</span><span>black |
| 72 | +</span><span> |
| 73 | +</span><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39;$-2d\nwq\n&#39; </span><span style="color:#72ab00;">|</span><span> ed </span><span style="color:#72ab00;">-</span><span>s colors.txt </span><span style="color:#72ab00;">- |
| 74 | +</span><span>$ cat colors.txt |
| 75 | +</span><span>red |
| 76 | +</span><span>green |
| 77 | +</span><span>yellow |
| 78 | +</span><span>black |
| 79 | +</span></code></pre> |
| 80 | +<h2 id="resource-links">Resource links</h2> |
| 81 | +<ul> |
| 82 | +<li><a href="https://blog.sanctum.geek.nz/actually-using-ed/">Actually using ed</a></li> |
| 83 | +<li><a href="https://raymii.org/s/tutorials/ed_cheatsheet.html">ed cheatsheet</a></li> |
| 84 | +<li><a href="https://jvns.ca/blog/2018/05/11/batch-editing-files-with-ed/">Batch editing files with ed</a></li> |
| 85 | +<li><a href="https://en.wikipedia.org/wiki/Ed_(text_editor)">wikipedia entry for the ed command</a></li> |
| 86 | +</ul> |
| 87 | +</content> |
| 88 | + </entry> |
10 | 89 | <entry xml:lang="en">
|
11 | 90 | <title>Perl One-Liners Guide book announcement</title>
|
12 | 91 | <published>2023-09-28T00:00:00+00:00</published>
|
|
0 commit comments