To add a line of text to an existing file follow the next example.
/home/larry# ed firstone.txt a This is a new line of text. . w q
If you check the file with cat you'll see that a new line was inserted between the original first and second lines. How did ed know where to place the new line of text?
When ed reads in the file it keeps track of the current line.
The command will add the text after the current line.
ed can also place the text before the current line with the key
command
. The effect will be the insertion of the text before
the current line.
Now it is easy to see that ed operates on the text, line by line. All commands can be applied to a chosen line.
To add a line of text at the end of a file.
/home/larry# ed firstone.txt $a The last line of text. . w q
The command modifier tells ed to add the line after the last
line. To add the line after the first line the modifier would be
.
The power is now available to select the line to either add a line of text
after the line number, or insert a line before the line number.
How do we know what is on the current line? The command key
will display the contents of the current line. If you want to change
the current line to line 2 and see the contents of that line then do
the following.
/home/larry# ed firstone.txt 2p q