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