Can You Use 'ed' As A Drop-in Replacement For vim, grep & sed?
2020-10-15 - By Robert Elder
Introduction
Vim, sed and grep are three very popular command-line tools for Linux platforms. This article will attempt to explain the origins of these tools by showing how they use a number of syntax features that are identical to an equivalent command that uses the ancient 'ed' editor which was written in the late 1960s.
For the purposes of demonstration, some example text (included at the end of this article) is contained in a file named 'lorem' which will be referenced by the commands found later in this article.
Grep As An 'ed' Command
Let's say that we want to find all lines in the file 'lorem' that contain the sequence of letters 'que'. You can do that with grep using this command:
grep "que" lorem
But, you could also do this by opening up the file 'lorem' with the 'ed' editor:
ed lorem
This will bring you to the command prompt of 'ed' where it will wait until you type a command that's specific to the 'ed' editor. Typing the following 'ed' command:
g/que/p
will cause 'ed' to find all lines that contain 'que' and print them out, followed by returning to the 'ed' command prompt.
We're now done working with 'ed' at this point, so we can type the 'q' command to exit, or just press Ctrl+d.
Since we only wanted to use 'ed' for its ability to search for lines with matching text, we can make this process a bit faster by just echoing an 'ed' command and piping it directly into 'ed' like this:
echo "g/que/p" | ed lorem
The above command will open up 'ed' on the file 'lorem', search through the files and print the matching lines, and then exit back to the shell prompt for us. We now have a way to produce output that is effectively as what we get from grep, but with 'ed'. The only minor difference is the character count that 'ed' prints out to stderr, which could be separately piped to /dev/null to ignore it:
echo "g/que/p" | ed lorem 2>/dev/null
Grep (With Regular Expressions) As An 'ed' Command
Since the last example was just a simple string search, you might assume the similarity is just a coincidence. However, this also works with most BRE-like regular expressions (the default regular expression type with grep). Here is a grep command that will search for all lines that contain a sequence of two 's' characters:
grep "s\{2\}" lorem
And here is the same search, but using the 'ed' command instead:
echo 'g/s\{2\}/p' | ed lorem
Since this pattern generalizes to most BRE-like regular expressions, you could use 're' to stand for 'Regular Expression' and the generic way of describing this use of ed would look like this:
echo 'g/re/p' | ed lorem
Notice, the 'g/re/p' part or 'grep'. Yup, that's where the name comes from. This is described in detail by Brian Kernighan in this YouTube video. 'grep' = the 'ed' command for 'Global Regular Expression Print'.
Sed As An 'ed' Command
'grep' is not the only common Linux tool that has been influenced by 'ed'. 'sed' is another example of a command-line tool where some of its syntax features can be verbatim copied into 'ed' to perform the same action. For example, here is a sed command that will find all lines in the file 'lorem' that contain a sequence of two 's' characters and replace them with two 't' characters. The result is then piped into the file '2' for later comparison:
sed '1,$s/s\{2\}/tt/g' lorem > 2
And using a similar pattern as before, here is an echo statement that will use the exact same argument that we passed to 'sed', but as an 'ed' command:
echo -e '1,$s/s\{2\}/tt/g'"\n"'1,$p' | ed lorem > 1
One difference in the command above is that we need to also echo a newline followed by another 'ed' command to actually print out the current contents of the file from inside the 'ed' editor. In the case of 'sed', the string replacement will run on every line and then immediately output the result. With the 'ed' command version, we're basically just saying "Open the file 'lorem' with 'ed', run the replacement rule on all lines in the file to replace 'ss' with 'tt', then print out every line of the file, then exit".
Now that we've edited the file 'lorem' using two different methods, let's diff the two resulting files '1' and '2' to see if there are any differences:
vim -d 1 2
In my case, the above command shows no differences. It's not always the case that every 'sed' command will work exactly the same as an 'ed' command like this, but by now you should hopefully see the similarities.
Vim As An 'ed' Command
Comparing 'vim' with 'ed' is where things should really start to make sense. If you've used vim before, you've most likely typed 'Esc' followed by ':' more than a few times. What you probably didn't know is that those ':' commands are actually called 'Ex' commands. The cool thing is, you can actually run any 'Ex' command automatically as soon as you open up vim from the command-line by using the '-c' flag. For example, if you want to open up the file 'a' in vim and immediately delete lines 10-20, you can use this command:
vim a -c '10,20d'
similarly, you can use this command to open up vim and them immediately copy or 'transfer' lines 4 to 7 to after line 15 using this command:
vim a -c '4,7t15'
You can also do the same thing with string replacement Ex commands. This will open up vim and immediately do a string replacement to replace 'Nunc' with 'ABC' on line 28:
vim a -c '28s/Nunc/ABC/'
Don't forget that saving and quitting vim is an Ex command too! Therefore, you can do this to open up vim and exit it immediately:
vim a -c 'wq'
Putting these all together gives us this vim command which opens vim, makes all 3 of the changes, then saves and quits. For the sake of example, the files 'a' and 'b' have the contents of the same 'lorem' file noted previously:
vim a -c '10,20d' -c '4,7t15' -c '28s/Nunc/ABC/' -c 'wq'
Now, here's the punch line: The exact same operations can be done with the 'ed' editor using this command:
echo -e '10,20d'"\n"'4,7t15'"\n"'28s/Nunc/ABC/'"\n"'wq' | ed b
The newlines in the command above are just to separate the individual 'ed' commands. If you were to do this without a pipe, you'd just open the file with 'ed' normally:
ed b
and then enter the following ed commands one after another:
10,20d
4,7t15
28s/Nunc/ABC/
wq
Now, you can use vim to compare the two files 'a' and 'b' to see if they're the same:
vim -d a b
In my case, they're identical.
Vim vs. 'Ex'
There is more to say about 'ex' commands. In fact, if you check the man pages for vim, you'll find that there is a '-e' flag that says it will 'Start Vim in Ex mode, just like the executable was called "ex"'. That's interesting, let's try launching vim with the -e flag:
vim -e lorem
The result is something that looks like this:
So clearly we're in vim, but we just have a command prompt that looks like the 'ed' editor. Let's try using an 'ed' command to print out all lines in the file and see what happens:
1,$p
Neat! It worked! So, the 'Ex' mode of vim is basically just an 'command-line interface' like editor that accepts 'Ex' commands, but really it's vim under the hood. It also happens to be the case that a lot (but not all) of the Ex commands are identical to 'ed' commands.
When launching vim in 'Ex' mode, you can get back to regular visual mode by typing:
visual
as an Ex command. To go the other way, from visual to 'Ex' mode, you can press Shift Q (not as an Ex command, just press it in visual mode):
Q
Reflecting back on the 'ex' command that was mentioned earlier in the man page, you can try just running a program called 'ex' and see what happens. You'll get the same result as with vim -e. In fact, it's worth checking if 'ex' is a symlink to something...
Is 'ed' As A Drop-in Replacement For vim, grep & sed?
So, to re-visit the question in the title of this article: "Can you use the 'ed' editor as a drop-in replacement for vim, grep & sed?" It wouldn't really be fair to answer 'Yes' to this question: The commands here were chosen because they are good examples of syntax that is identical between the two context, however it isn't that hard to find a few counter-examples where it doesn't quite work. For example, things like cutting and pasting are different between 'ed' and vim, and some of the range syntax that sed accepts is not accepted by 'ed', however the overall similarities are still there. Having said this, the answer to this question isn't quite 'No' either: Depending on what you want to accomplish with vim/grep/sed, you may actually be able to get away with using an identical (or very similar) command in the 'ed' editor as demonstrated in this article.
In the end, the purpose of this article is not to convince you that all of the features in vim/grep/sed are exactly replicated in 'ed', but rather to show how these common Unix tools have a lot more in common than most people realize.
The Example Text File 'lorem'
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean erat
2 nisi, convallis vel justo sed, pretium maximus libero. Proin urna
3 eros, maximus nec dolor a, laoreet tristique magna. Integer bibendum
4 vestibulum rutrum. Suspendisse vulputate ligula dui, a dictum sapien
5 suscipit in. Nam consequat dapibus leo nec porta. Nam eu odio ut enim
6 egestas porttitor. In consectetur nisi nec ex rutrum, et aliquam metus
7 bibendum. Curabitur non libero augue.
8
9 Aliquam porta mauris ut nibh rutrum, eu sollicitudin ligula
10 sagittis. Sed consequat dui quis augue pretium viverra. Ut fermentum
11 lectus turpis. Curabitur vel mi urna. Pellentesque est lorem, ullamcorper
12 eget libero sit amet, viverra convallis elit. Pellentesque at erat congue,
13 tempus enim quis, finibus turpis. Phasellus mauris metus, vulputate ut
14 nibh sit amet, tristique imperdiet massa. Donec nec quam nec nunc feugiat
15 lacinia. Suspendisse rutrum vehicula justo vitae pellentesque. Nulla
16 vitae neque dapibus, auctor eros vitae, mattis neque. In hac habitasse
17 platea dictumst. Praesent ante massa, iaculis eget felis eget, tempor
18 fermentum orci. Suspendisse et ipsum vel arcu efficitur efficitur. Nam
19 dapibus magna non nulla fermentum semper.
20
21 Suspendisse pretium lorem pellentesque iaculis convallis. Ut euismod
22 nisl mollis tincidunt auctor. Vivamus feugiat leo sed elementum
23 vulputate. Proin porta nisi at lacinia lacinia. Suspendisse felis
24 augue, pharetra sit amet eleifend ut, sagittis nec metus. Maecenas
25 accumsan fringilla ex, at iaculis ante eleifend ac. Praesent vel
26 faucibus felis. Donec vehicula ex ornare enim ultrices lacinia quis eu
27 tellus. Donec sagittis, ligula imperdiet maximus convallis, lorem purus
28 sagittis leo, et eleifend tellus turpis id eros. In bibendum a mi et
29 porta. Proin eget pretium erat. Sed vulputate pellentesque metus, sed
30 pulvinar dui posuere nec. Sed efficitur maximus nisi at placerat. Mauris
31 imperdiet vitae turpis nec maximus. Praesent laoreet facilisis libero,
32 sed finibus mauris bibendum a. Nam suscipit, eros et eleifend rutrum,
33 magna ligula efficitur ligula, sed dictum sapien sapien eget massa.
34
35 In dictum porta molestie. Nunc a lobortis mauris, in ultricies
36 risus. Vestibulum id ligula lacinia, maximus ligula ut, tincidunt
37 mi. Aliquam nunc dolor, tempus a enim eu, tempor pretium nulla. Vivamus
38 ac volutpat eros. Etiam ut pellentesque libero. Vivamus mattis turpis
39 leo, quis facilisis turpis blandit nec. Duis ultricies, nisi sit amet
40 efficitur ultrices, odio arcu aliquam erat, et feugiat arcu tellus vel
41 eros. Aenean rhoncus nibh a est eleifend vehicula. Cras at dui ac sem
42 feugiat rutrum ac id metus.
43
44 Praesent pharetra iaculis lacus, vitae pretium mauris consequat ac. Aenean
45 a nulla aliquam, egestas odio et, ultricies ex. Proin sit amet maximus
46 nulla. Integer nibh augue, consequat et placerat vel, condimentum id
47 nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin
48 feugiat est tortor, eget lobortis ex cursus a. Lorem ipsum dolor sit amet,
49 consectetur adipiscing elit.
The Most Confusing Grep Mistakes I've Ever Made
Published 2020-11-02 |
$1.00 CAD |
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
Published 2020-10-05 |
Undefined Behaviour With Grep -E
Published 2020-10-01 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Use The 'tail' Command To Monitor Everything
Published 2021-04-08 |
An Overview of How to Do Everything with Raspberry Pi Cameras
Published 2019-05-28 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|