Professional Documents
Culture Documents
echo
grep
sed
cut
- uniq
- sort
- diff
- pr
- find
Editing
vi
- emacs
Shell Variables
Environment Variables
11
echo
to show stuff on the screen
Prints to standard output
echo hello
hello
echo A short
A short line
echo A short
A short
(in newfile)
line
(in newfile)
hello=Hi, there
Hi, there
echo \hello\
hello
22
Echo
(with variables and quotes)
echo $hello
blank
Define variable:
echo $hello
hello=Hi, there
Hi, there
echo $hello
Hi, there
echo \$hello
$hello
echo $hello
$hello
33
grep
Find Pattern in File(s)
cut
To Get Pieces Of Each Line
cut
cut
cut
cut
cut
tr Examples
To translate single characters to single characters
tr A-Z a-z
or tr A-Z a-z
or tr [A-Z] [a-z]
translates upper case to lower case
tr -s \t :
replace multiple spaces+tabs with single colon
6
sed
To Substitute in a File
sed s/string/newstring/g file > newfile
sed s?relative/path/name?new/dir/name?
file > newfile
sed 5,10s/string/newstring/ file > newfile
sed /pat1/,/pat2/s/strg/newstrg/ file > nf
77
sed
Delete Lines
(You can also add lines, but >> is easier)
sed '5,10d' file > newfile
Deletes lines 5 through 10
uniq
Eliminate Duplicate Lines
uniq file > newfile
eliminate duplicates (must be adjancent)
99
sort
sort file > newfile
ls -l | sort -r -k 5n | less
show largest files)
11
00
Comparing Files
diff file1 file2
pr
Print Command
pr filename
format for printing
pr filename | lpr
actually print formatted file
flags:
+k
-k
-h
-t
find
find . -name filename -print
find a file with the name filename
awk
This is an extra. I will not test on awk, but
it may be useful in some of the quiz and
homework questions.
awk is more than a command, it is
essentially a language. But if you want the
full power, I would suggest learning and
using Perl instead. Though harder to learn,
it is much more powerful, and does all that
awk does.
1
1
44
awk definition
AWK is a language for processing text files. A file
is treated as a sequence of lines, called records.
Each line is broken up into a sequence of fields,
so we can think of the first word in a line as the
first field, the second word as the second field,
and so on. An AWK program is of a sequence of
pattern-action statements. AWK reads the input
a line at a time. A line is scanned for each
pattern in the program, and for each pattern
that matches, the associated action is executed.
- Alfred V. Aho (one of the inventors of awk,
quoted in Wikipedia, modified by me)
1
5
awk examples
1.
2.
3.
4.
5.
11
77
enter vi
go into insert mode
ESC
ZZ
(key)
(uppercase)
22
00
vi Concepts
Modes
command (ESC)
insert (i, a, o, etc)
ex mode (:)
vi modes
Command mode
What you type is a command to vi
ESC will change you from insert to command
ex mode
Commands to ex editor, start with :
On bottom line of screen
22
22
ex mode
s, d, undo, w, q
:undo undo last vi operation
:w <filename> write file (:w! to force)
:q quit (:q! to force)
:d delete current line
:5,10d delete lines 5 through 10
addressing for ex
22
55
To exit vi
ZZ save file and exit
DO NOT confuse with ^Z, which puts it in the
background
22
66
I Insert at
beginning
o Open
new line
below
Cursor
A Append at
end
Insert
before
cursor
Append
after
cursor
22
77
ZZ
INSERTING TEXT
I - insert text before
cursor
o - insert text below
current line
a - insert text after
cursor
O - insert text above
line
22
88
vi Cursor Movements
1G
K
O
Cursor
[]
E
[] Try these,cursor,commands ,
in a
+
Return
file.
b l
h
G
2
9
vi Screen Movement
H
Basic VI COMMANDS:
To enter VI type:
vi filename <cr>
^b
ZZ
^f
INSERTING TEXT
I - insert text before
cursor
o - insert text below
current line
a - insert text after
cursor
O - insert text above
line
L
33
00
dd Delete
line
character
at cursor
3
1
emacs Concepts
Commands use special keys: control (CTRL),
escape (ESC) or meta or alt, function keys
Style modes (but not like vi)
Buffers (multiple windows into a file)
Extensible (in Lisp)
Unlimited undo
Keyboard macros; key binding
Files can be large (doesnt read them into core)
33
22
(emacs concepts)
Automatic backups
Can set automatic saving
33
33
emacs filename
C-x C-s
C-x s
C-c C-c
(edits filename)
save file and exit
save all files and exit
exit (will ask if you want to)
33
44
emacs help
C-h
Processes
33
66
Shell Variables
Name and Value
Examples
MyName=Isaacs
HOME=isaacs
fruit=apple
MONTH=April
fullname=Stan Isaacs
33
77
USES
Environment Variables
to define your environment
Local Variables
inside scripts
long names
Environment Variables
Usually all caps
Represent your personal environment
Used by various UNIX programs and commands
HOME
LOGNAME
PATH
EDITOR
LPDEST
PRINTER
TERM
PS1
44
00
Environment Variables
env to see them all
You can change some of them
frequently in startup scripts
Example
d=/usr/isaacs/class/day1/exercises
cd $d
pr $d/ex3
44
22
Summary of Variables
Case sensitive
No spaces around =
Quote if there are spaces in value
Curly braces ({,}) to separate
Usually character strings
num=100.0 is different from num=100)