
The ECN No Name Newsletter is no longer being published. This is an archived issue.
[previous article] [next article]Stacey Clark
There are special characters you can use in VI and EX:
$ = end of line
^ = beginning of line
. = match any one character
* = repeat (r* could be r, rr, rrrrr, etc.)
.* = everything on the line
a.*z = everything from the first a on a line
to the last z on a line
Suppose you just got all new Sun accounts, and you wanted to create a .rhosts file to transfer files easily back and forth between machines without having to type in your password.
The first step is to log in to a Sun and determine what machines are on the local network. To accomplish this type the ruptime command and redirect the output into your .rhosts file with "ruptime > .rhosts". Then use the EX or VI editor to remove unwanted hosts and modify the file. Remember the ruptime output will depend upon where you are when you type the command. Your output might look like:
argon;up 20+18:34,;0 users,;load 0.02, 0.00, 0.00
atoms;up 25+03:16,;1 user,;load 0.97, 1.70, 1.72
berkelium;up 141+17:48,;0 users,;load 0.52, 0.30, 0.02
bromine;up 20+00:53,;4 users,;load 0.21, 0.36, 0.09
chlorine;up 26+01:33,;5 users,;load 0.44, 0.30, 0.03
cn;up 44+07:40,;23 users,;load 3.72, 3.68, 3.65
... etc.
Now you want to convert the information gathered (machine names plus all the other stuff) into something that looks like:
argon.ecn.purdue.edu stacey
except with your login instead of "stacey".
Achieving this deletion/replacement procedure can be a single or multiple step process depending on how you elect to use the special matching characters presented earlier.
First erase everything to the right of the machine name (which is everything after the first space) by typing ":1,$s/ .*//". This will produce:
argon
atoms
berkelium
bromine
chlorine
cn
...etc.
There are many ways to specify "all lines."
Use the method that is easiest for you to remember.
Next, add your desired text to the end of each line. Using the dollar sign twice in the substitute command, I am telling VI and EX to perform the asked for substitution to all lines ":1,$s" (from the first line (1) through the last line of the file ($) substitute (s)) and then to append to the end of the line "/$/" the text string ".ecn.purdue.edu stacey".
":1,$s/$/.ecn.purdue.edu stacey/"
and produces:
argon.ecn.purdue.edu stacey
atoms.ecn.purdue.edu stacey
berkelium.ecn.purdue.edu stacey
bromine.ecn.purdue.edu stacey
chlorine.ecn.purdue.edu stacey
cn.ecn.purdue.edu stacey
To made the changes with one command, type:
":%s/ .*/.ecn.purdue.edu stacey/"
Remember the substitute command is basically:
starting line number, ending line number substitute / old / new /
:1,55 s /dog/bird/