2. VI EDITOR
Vi editor is a visual screen editor which is available in almost all Unix systems. It is a
fast powerful editor.
Starting vi-
๏ to Start vi editor just type vi followed by the filename. Is the file is already exists
then it will show the contents of the file and allow you to edit them and if the file
dosent exist then it will create a new file.
๏ $ vi filename
3. MODES OFVI
1. Command mode: in this mode only commands will work to arrange, copy or
delete the data.Vi always start out in command mode and if you are working
and then you want to switch it back in command mode press ESC.
2. Insert mode: in this mode data can be enter into the file. By pressing โIโ will shift
you into the insert ode from command mode.
General command information:
Vi commands are
๏ Case sensitive
๏ Are not displayed on the screen when we type them.
๏ Generally do not require return after we type the command.
4. MOVING ONE CHARACTER AT ATIME:
h Left one space
i Right one space
j Down one space
k Up one space
5. MOVING AMONG WORDS AND LINES:
w Moves the cursor forward one word
b Moves the cursor backward one word
e Moves to the end of a word
ctrl+f Scrolls down one screen
ctrl+b Scrolls up one screen
ctrl+u Scrolls up half a screen
ctrl+d Scrolls down half a screen
Shortcuts: Two shortcuts for moving quickly on a line include $ and 0.
$ - will move us to the end of a line.
0 - will move us to the beginning of a line.
Screen Movement:
6. x Deletes the character under the cursor
X Deletes the character to the left of the cursor
dw Deletes the character selected to the end of the word
dd Deletes all the current line. (7dd โ will deletes 7 lines)
D Deletes from the current character to the end of the line.
Yw Copies a word into buffer (7yw will copy 7 words)
Yy Copies a line into buffer (7yy will copy 7 lines)
P Will paste all the copied things.
Deleting Characters, Words and Lines:
Copying and Pasting text:
7. u Undoes the last change we made anywhere in the file
U Undoes all the recent changes to current line.
J To join lines (4J - to join 4 lines)
:w To save your file but not quit vi
:q To quit if we have not made any edits
:wq To quit and save edits
ZZ To quit and save edits (same as above)
:!q Force quit ( when it is not allowing you to quit file do force quit)
Quitting and Saving a File
Joining and Undoing a line
8. / [string] Search forward for string
? [string] Search backward for string
N Repeat last search
:1,10w file Write lines 1 through 10 to file newfile
:sh Escape temporarily to a shell
^d Return from shell to vi
:set number Show line numbers
:set all Display current values of vi
Searching and Substitution Commands
Other Commands:
9. PROGRAMMING WITH AWK
๏ Awk is not just a command but a programming language too. It uses unusual
syntax that uses two components and requires single quotes and curly braces.
awk options โselection criteria {action}โ files
๏ The selection criteria (a form of addressing) filters input and selects lines for the
action component to act on.This component is enclosed within curly braces.
๏ The address (rather than selection criteria) and action constitutes and awk
program that is surrounded by a set of single quotes.
awk โF: โ$3>200โ { print $1, $3 }โ /etc/passwd
Selection
Criteria
Action
Use to split fields with white spaces or on the delimiter specified with โF option
10. SPLITTING A LINE INTO FIELDS
๏ AWK breaks-up a line into fields on whitespaces or on the de-limiter specified with
the โF option. These fields are represented by the variables $1, $2, $3 and so forth.
$0 represents the full line.
๏ Since these parameters are evaluated by the shell in double quotes, awk
programs must be a single quoted.
$awk โFโ|โ โ/sales/ { print $2,$3,$4 } empn.lst
In the above line, awk will fetch all the records contains keyword โsalesโ and will
print second, third and fourth word from empn.lst.
11. COMPARISON OPERATORS
๏ $awk โFโ|โ โ$3 == โdirectorโ || $3==โchairmanโ empn.lst
- Prints the complete line.
๏ $awk โFโ|โ โ$3 == โdirectorโ || $3==โchairmanโ { printf โ%-20s %-12s %dn, $2,$3, $6 }โ
empn.lst
-prints the specific words.
John woodcook director 120000 20939
Barry wood chairman 160000 27288
John woodcook director 120000
Barry wood chairman 160000
Bill Johnson Director 130000
12. โฆ
๏ $awk โFโ|โ โ$3 == โdirectorโ || $3==โchairmanโ { printf โNR, %-20s %-12s %dn,
$2,$3, $6 }โ empn.lst
-NR is a new keyword which prints the line number.
๏ $awk โFโ|โ โ$6 > 120000 { printf โNR, %-20s %-12s %dn, $2,$3, $6 }โ empn.lst
13 John woodcook director 120000
15 Barry wood chairman 160000
19 Bill Johnson Director 130000
13. COMPARISON OPERATORS
< LessThan
<= Less than or equal to
== Equal to
!= Not Equal to
>= Greater than or equal to
> Greater than
~ Matches a regular expression
!~ Does not match a regular expression
14. NUMBER PROCESSING
๏ Awk can perform computations on numbers using the arithmetic operators +, -,
*, / and % (modulus).
๏ Salespeople often earn a bonus apart from salary. We will assume here that the
bonus amount is equal one monthโs salary.
$awk โFโ|โ โ$4 == โsalesโ { printf โNR, %-20s %-12s %6d %8.2fn,
NR,$2,$3,$6,$6/12 }โ empn.lst
13 John woodcook director 90000 7500.00
15 Barry wood chairman 140000 11666.67
19 Bill Johnson Director 110000 9166.67
15. VARIABLES
๏ AWK has certain built-in variables like,
๏ NR
๏ $0
๏ AWK provides user a flexibility to define own variables. It has two special feature-
๏ No type declarations are needed.
๏ By default and depending on its type, variables are initialized to zero or a null string.
AWK has a mechanism of identifying the typeof a variable used from its context.
$awk โFโ|โ โ$4 == โsalesโ {
>kount = kount + 1
>printf โNR, %-20s %-12s %6d %8.2fn,kount,NR,$2,$3,$6,$6/12 }โ empn.lst
1 13 John woodcook director 90000 7500.00
2 15 Barry wood chairman 140000 11666.67
16. BUILT-INVARIABLES
NR Cumulative number of lines read
FS Input field separator
OFS Output field separator
NF Number of fields in the current line
FILENAME Current input file
ARGC Number of arguments in command line
ARGV List of arguments
17. THE BEGIN AND END SECTIONS
๏ If you have to print something before you process the input, the BEGIN section
can be used quite gainfully. Similarly, the end section is equally useful for printing
some totals after the processing is over.
๏ BEGIN { action }
๏ END { action }
18. LETโS DO A PROGRAM
๏ $cat emawk2.awk
BEGIN { printf โntt Employee Detailsnnโ
}
$6 > 12000 { #Increment variables for serial number and pay
kount++; total+-$6
printf โ%3d %-20s %-12s %snโ, kount,$2,$3,$6
}
END { printf โntThe average salary is %6dnโ, total/kount
}
19. EXECUTION & OUTPUT
๏ $ awk โFโ|โ โf empawk2.awk empn.lst
Employee Details
The average salary is 115000
1 John woodcook director 90000
2 Barry wood chairman 140000
20. ARRAYS
๏ Awk handles only one-dimensional arrays.
๏ No array declaration is required.
๏ It is automatically initialized to zero unless initialized explicitly.
$ cat empawk4.awk
BEGIN { FS = โ|โ ; printf โ%40snโ, โSalary Commissionโ }
/sales/ {
commission = $6*0.20
tot[1]+=$6; tot[2]+=commission
kount++;
}
END { printf โT Average %5d %5dnโ, tot[1]/kount, tot[2]/kount
}
OUTPUT
$ awk โf empawk4.awk empn.lst
Salary Commission
Average 105625 21125
21. FUNCTIONS
๏ AWK has several built-in functions performing both arithmetic and string
operations.
$ awk โFโ|โ โlength($2) < 11โ empn.lst
It will search those people who have short names.
int(x) Returns integer value of x
sqrt(x) Returns the square root of x
length Returns length of complete line
length($x) Returns length of x
substr(stg,m,n) Returns portion of string length n, starting from position m in a string stg
index(s1,s2) Returns position of string s2 in string s1
split(stg,arr,ch) Split string stg into array arr using ch as delimiter; optionally returns number of fields
system(โcmdโ) Runs UNIX command cmd, and returns its exit status