15.1.4.5 Other Actions

15.2 Editing a File Using the vi Editor

In this section you will learn how to edit a file. You will learn how to replace multiple characters, replace one character, delete a character, delete a line, copy a line, find a word, insert characters, and insert a line.

In this example you have a file called yourfile. In this file you are running a few simple commands and sending the output for each to your screen.

The yourfile file looks like this:

#
# $HOME/yourfile
#
# This file contains some commands that are run by the user.
df
lsfs rootvg
lslv hdisk1
lsdev -Cc disk

15.2.1 Inserting Text Using the vi Editor

As you look at the file above you see that you want the df command to display the filesystem space in 1024 KB blocks, not 512. You also want the script to list what is in the current directory at the end of the script. To edit the file enter:

vi yourfile

Figure 124 is the display for the vi editor.



Figure 124: Image of yourfile after vi Command Was Run

You will note that the cursor is at the top left of the screen under the first character. Move your cursor down five lines to under the df using one of the following keys - Down Arrow or j or Ctrl-J or Ctrl-N. There are now two options for you to add the -k flag to the df command.

Option 1

Go to the end of the text using Shift-A, which appends to the end of the line. You will notice that your cursor is to the right of the f, press the Space Bar and insert the -k.

Option 2

Go to the end of the line using one of the following keys - Right Arrow or l or $. Your cursor will now be under the f, press the a key, your cursor will move one space to the right, press the Space key and then insert the -k.

Your line will look like this,

df -k

Press the Esc key twice; you will hear a single beep. You have now moved out of edit mode back into command mode. Your cursor will be under the k.

Now add the ls -l of the current directory at the end of the file. Using any one of the Down Arrow or j or Ctrl-J or Ctrl-N keys, go to the bottom of the file, the cursor will be under the v in the lsdev command, either use the Left Arrow or h or Ctrl-H or 0 keys to move to the beginning of the line. Your cursor should be under the l of the lsdev command. To insert a new line below the current line press o, you will see the cursor under the l of the lsdev command but one line down, type the following:

s -l

Press Esc twice to go back into command mode. You will notice that this command is incorrect. What you need to do is insert the l for the ls -l command, press 0 to take you to the beginning of the line, your cursor is under the s, press the i key then type:

l

Press Esc twice to go back to command line. Your line should look like this:

ls -l

To save the file there are two options to save and quit.

Option 1

Hold the Shift and ; ( : ), the cursor will go to the bottom left of the screen next to a :. At the : you can type wq!, which overwrites the original file and then quits.

Option 2

Hold the Shift and press z twice (ZZ). This will save and quit, taking you back to your AIX command prompt.

To view the file type:

# cat yourfile

The output of the cat command is as follows:

#
# $HOME/yourfile
#
# This file contains some commands that are run by the user.
df -k
lsfs rootvg
lspv hdisk1
lsdev -Cc disk
ls -l

15.2.2 Changing Text Using the vi Editor

On closer inspection of the file yourfile you notice that there are a few mistakes, and you now want to correct them. To correct them type:

vi yourfile

You will see the cursor at the top right side of your monitor.

The first thing you want to do is fix the lsfs command, you do not want it to be lsfs because it should be the lsvg command. You also want to check the volume group uservg not rootvg.

Press any of the Down Arrow, j, Ctrl-J, or Ctrl-N keys until you are under the l of the lsfs command.

You can also use the find option. Press the / key, it will take you to the bottom right of the screen and you will see a / before the cursor, here you can type lsfs and it will take you to the beginning of the lsfs command.

Using the Right Arrow or l key move the cursor below the f of the lsfs command. Press the Shift key and r (R) simultaneously. The cursor will still be under the f of the lsfs command. Type vg; you will see that it has overwritten the fs with vg. Press Esc twice to go back into command mode. Your cursor is now under the g in the lsvg command.

Using the Right Arrow move the cursor to underneath the r of rootvg. Type cw, and you will see a $ sign at the end of the word rootvg. Type uservg; you will notice that it has overwritten rootvg. Press Esc twice and your cursor will be under the g of uservg.

Issue the command lspv on both hdisk1 and hdisk0. Press Down Arrow once to go down one line. You will notice that your cursor is under the 1 of hdisk1. Press your y key twice (yy), you have just yanked or copied the line. Press p key to place your yanked line and you will see that the line has been duplicated.Notice that your cursor is at the beginning of the new line. Your file should look like this:

lspv hdisk1
lspv hdisk1

Using the Right Arrow key move your cursor until it is under the 1 of the second hdisk1. Use your Up Arrow key to go up to the first hdisk1. Press the r key and then the 0. You will notice that the 1 has changed to a 0. You do not need to press the Esc key since this is a single character replacement. Your lines should now look like this:

lspv hdisk0
lspv hdisk1

Looking at the line with the lsdev -Cc disk command, you decide that you want this command to display all the devices, not just the disks. Using the Down Arrow go to the line with the lsdev command. Using the Right Arrow key put the cursor under the c, press the x and you will see the c disappear. Use the Right Arrow to go under the d of the word disk. Press the d key twice (dd), you will notice the whole line has disappeared. Press the u key once and you will notice that the line has come back. Now press the d key, then the w key. The word disk has now disappeared. Press Esc twice. Your line will now look like this:

lsdev -C

Press the Shift zz (ZZ) to save and exit.

Type the command:

cat yourfile

to display your file. It should appear like this:

#
# $HOME/yourfile
#
# This file contains some commands that are run by the user.
df -k
lsvg uservg
lspv hdisk1
lspv hdisk0
lsdev -C
ls -l

To replace multiple occurrences of text you can use the s subcommand. In the above example if you wanted to change the lspv commands to have the -p flag, edit the file using vi and then change the command using a subcommand as follows:

vi yourfile

The cursor will be at the top left hand side. Type Shift and ; (:), the cursor will go into the command line editor at the bottom left. To edit type:

%s/lspv/lspv -p

This will replace all lspv commands to lspv -p. Save the file and exit.

Type the command:

cat yourfile

to display your file. It should appear like this:

#
# $HOME/yourfile
#
# This file contains some commands that are run by the user.
df -k
lsvg uservg
lspv -p hdisk1
lspv -p hdisk0
lsdev -C
ls -l

15.3 Viewing Files and String Settings