My friend ed
This month we’re going to look a little closer at working with files. Beyond simply creating files, it is also important to know how to edit them. If you take a look at most Unix textbooks or MacOS X books you probably won’t find a reference to my favorite editor, “ed”. The editor that gets the most press is “vi”, I have found “ed” the simplest to learn, easiest to use and elegant in its simplicity.
The most interesting part of working with MacOS X and it’s Unix underpinnings, as I have said in past articles, is the applications that come with the OS. You wont have to send any money to the folks in Seattle or pay any shareware fees. If you’re running Unix you already have “ed”.
Before we get started on “ed” lets have a look at what we’ve learned so far and add a few new tricks. We’ve looked at an application called “cat”. Short for concatenate, “cat” will “print” the contents of any file in your Terminal window. (Applications >> Utilities >> Terminal.) You should also remember that, you start working in your home directory when you start your Terminal app.
You can confirm where you are at any time by typing “pwd” and “Return” (or “Enter”.) “pwd” returns your “present working directory” which is always helpful, as you’re navigating without the familiar Windows and directories on your Mac or PC. What I see when I run “pwd” is:
/Users/tim
Under MacOS X our home directories are always in the “Users” directory which is under the root directory “/”. When I list the contents of my home directory with “ls” I can see all the files and folders I have stored. Get in the habit of using “pwd”, “ls” and “ls la” and you will always know where you are what your doing.
If you’ve been with us since October, you may still have a file called “text.txt” that we created. Here’s a different way to create a new file. At your command prompt type this:
cat > newtest.txt
here's some text
To end the cat program enter “control-d”. You will then get a new prompt. Use
“ls la” and you will see a new file called “newtest.txt”. The “>” is used to input any text that follows into the file “newtest.txt” that we created with the “cat” application.
To add or append more text to our file we can do the following. Type this:
cat >> newtest.txt
and now a second line
End again with “control-d”. You can also use “control-c” to end any program that you’re running. Once again you can see the contents of the file simply using “cat” without the “greater than” symbols:
cat newtest.txt
here's some text
and now a second line
We’ve seen a few ways to create files and add data, so now we can use “ed” to edit our files. “ed” is a line editor, so you will work on one line at a time. Type this:
ed newtest.txt
The screen will print “39″ which is the number of characters in this file. To see the contents of the first line enter “1″ and “Return”. “ed” will return this:
here's some text
Enter “2″ or simply hit “Return” again and “ed” will print the next line:
and now a second line
You can see the whole file at once by entering “1,$p” the following will be displayed:
here's some text
and now a second line
What you’ve asked “ed” to do is; starting at the first line “1″ until the end of the file “$”, print “p”. The “$” usually indicates the “end” (The “^” also means the start, but in this case the start of a line.) You might think that we could have entered “^,$p” but that would confuse “ed”. When “ed” doesn’t understand or if it encounters an error it replies with a question mark “?’.
To add more text to your file you might append with “a”. To start appending enter “a” and enter the new text. When you are finished appending, enter a period on a line by itself.
a
this is the third line
and this is the fourth
.
By entering “a” (or “i” insert, or “c” change”) “ed” enters input mode. The period on a line by itself ends the input mode. Insert “i” will enter the new text before the current line, and “change” “c” will replace the current line. To look at the whole file again, I enter “1,$p”:
here's some text
and now a second line
this is the third line
and this is the fourth
Remember you can go to any line by entering it’s number. You can search through a file with a forward slash “/” and the text your searching for. Type this:
/second
and “ed” will print the first occurrence of “second”. Enter “/” again and “ed” will find the next occurrence. (If there is one.)
To make a change to a line, start to substitute with “s”, then type which word or phrase to be replaced between slashes. Type the replacement text next and close with a slash. Don’t forget to enter “p” to print the line. (eg. s/this/that/p) We should still be on the second line, so enter this:
s/a second/another modified/p
Our second line now reads “and now another modified line”. If you make a mistake you can undo your change with “u” (and “p” to print.) Type this:
up
“ed” returns “and now a second line” . The second line is restored. To duplicate a line enter “t.p” The “t” duplicates the current line “.” and again we print it. Entering “d” will delete the entire line. Be careful with that command. I usually use it with a “p”, and don’t forget you can undo if you make a mistake.
To save the changes to your file, enter “w” to write the file. Of course you can only save a file that you have permission to write to the file. To end your session with “ed”, enter “q” to quit. If you haven’t saved your work “ed” will prompt you with a “?”
Before I let you go here’s a couple of more features of “ed”. While you’re working on a file you can hit “f” and “ed” will print the name of the file your working on. It’s handy to check the filename now and again.
You could also begin your session with a brand new file. From the command prompt enter:
ed brandnewfile.txt
“ed” will print “0″ indicating that this is a new file with nothing in it. To start entering text type “a” and return, enter your copy, and remember to end with a period.
There are a just a few more things that I’ll cover later. This article has covered the basics, yet I think you will find that, since these are most of the commands available, “ed” is a really simple program. It will become the “Swiss Army Knife” of your Unix editing repertoire. You won’t find it covered in most texts, so it can be our little secret. Now off you go. Practice creating and modifying a few files on your own.










