2. L2B Second Linux Course
L2B Linux course
Session outlines:
Text Processing Tools
Text Processing Tools Exercises
VIM
VIM Exercises
Basic System Configuration Tools
4. L2B Second Linux Course
L2B Linux course
Extracting Text
cat
• One or more files
less
• Easy to read
• /text
• n/N
• v open an editor
• Used by man command to present man pages
5. L2B Second Linux Course
L2B Linux course
Extracting Text
head
• First 10 lines only
• -n change number of lines
tail
• Last 10 lines only
• -n change number of lines
• -f monitoring the file
6. L2B Second Linux Course
L2B Linux course
Extracting Text
cut
• Display specific columns
• -d column delimiter
• -f number of field
• -c cut by characters
– cut -c2-5 /usr/share/dict/words
7. L2B Second Linux Course
L2B Linux course
Extracting Text
Grep
• Display lines where a pattern is matched
• -i case-insensitively
• -n print line numbers of matches
• -v print lines not containing pattern
• -AX include the X lines after each match
• -Bx include the X lines before each match
8. L2B Second Linux Course
L2B Linux course
Tools for Analyzing Text
wc
• Counts words, lines, bytes and characters
• -l only line count
• -w only word count
• -c only byte count
• -m character count (not displayed)
9. L2B Second Linux Course
L2B Linux course
Tools for Analyzing Text
sort
• Sort text and original file is not changed
• -r reverse (descending) sort
• -n numeric sort
• -u (unique) removes duplicate lines
• -t c field separator
• -k X which field
• uniq & uniq -c
10. L2B Second Linux Course
L2B Linux course
Tools for Analyzing Text
diff and patch
• diff foo.conf-broken foo.conf-works
• -u better for patchfiles
• Patching
– diff -u foo.conf-broken foo.conf-works > foo.patch
– patch -b foo.conf-broken foo.patch
11. L2B Second Linux Course
L2B Linux course
Tools for Analyzing Text
aspell
• Interactively spell-check files:
– aspell check letter.txt
• Non-interactively list mis-spelled words in
– Only reads data from standard input
– aspell list < letter.txt
12. L2B Second Linux Course
L2B Linux course
Tools for Manipulating Text
tr and sed
13. L2B Second Linux Course
L2B Linux course
Tools for Manipulating Text
Tr
• Converts characters in one set to corresponding
characters in another set
• Only reads data from STDIN
• tr 'a-z' 'A-Z' < lowercase.txt
14. L2B Second Linux Course
L2B Linux course
sed (stream editor)
search/replace operations on a stream of text
Normally does not alter source file
-i to alter source file
-i.bak to back-up and alter source file
15. L2B Second Linux Course
L2B Linux course
sed Examples
sed 's/dog/cat/i' pets
sed 's/dog/cat/g' pets
sed '1,50s/dog/cat/g' pets
sed '/digby/,/duncan/s/dog/cat/g' pets
sed -e 's/dog/cat/' -e 's/hi/lo/' pets
16. L2B Second Linux Course
L2B Linux course
Characters for Complex Searches
^ represents beginning of line
$ represents end of line
Character classes as in bash:
• [abc], [^abc]
• [[:upper:]], [^[:upper:]]
Used by: grep, sed, less, others
18. L2B Second Linux Course
L2B Linux course
VIM Advantages:
Speed: Do more with fewer keystrokes
Simplicity: No dependence on mouse/GUI
Availability: Included with most Unix-like OSes
Disadvantages
Difficulty: Steeper learning curve than simpler
editors
19. L2B Second Linux Course
L2B Linux course
Three main modes:
Command Mode (default): Move cursor,
cut/paste text
Insert Mode: Modify text
Exit Mode: Save, quit, etc
20. L2B Second Linux Course
L2B Linux course
First steps with vim
vim filename(command mode)
Insert mode
• I at the cursor
• A append to end of line
• I insert at beginning of line
• o insert new a line (below)
• O insert new line (above)
Exit mode
• :w writes (saves) the file to disk
• :wq writes and quits
• :q! quits, even if changes are lost
21. L2B Second Linux Course
L2B Linux course
Command Mode
Right Arrow moves right one character
5, Right Arrow moves right five characters
Arrow Keys, h, j, k, l (the same as arrows)
w, b Move by word
), ( Move by sentence
}, { Move by paragraph
xG Jump to line x
gg Jump to the first line
G Jump to the last line
22. L2B Second Linux Course
L2B Linux course
Command Mode
As in less
• /, n, N
As in sed
The selected line only
• :1,5s/cat/dog/
All of the entire file
• :%s/cat/dog/gi
23. L2B Second Linux Course
L2B Linux course
Command Mode
Line
• cc
• dd
• yy
Letter
• cl
• dl
• yl
24. L2B Second Linux Course
L2B Linux course
Command Mode
Word
• cw
• dw
• yw
Sentence ahead
• c)
• d)
• y)
25. L2B Second Linux Course
L2B Linux course
Command Mode
Sentence behind
• c(
• d(
• y(
Paragraph above
• c{
• d{
• y{
26. L2B Second Linux Course
L2B Linux course
Command Mode
Paragraph below
• c}
• d}
• y}
u undo
Ctrl-r redo
U undo all changes to the last modified line
27. L2B Second Linux Course
L2B Linux course
Command Mode
v starts character-oriented highlighting
V starts line-oriented highlighting
Visual keys used with movement keys:
• w, ), }, arrows
Highlighted text can be
• Deleted
• Yanked
• Changed
• Filtered
• search/replaced, etc.
28. L2B Second Linux Course
L2B Linux course
Command Mode
Ctrl-w, s splits the screen horizontally
Ctrl-w, v splits the screen vertically
Ctrl-w, Arrow moves between windows
Ctrl-w twice, Arrow moves between windows
To search for help inside vim convert to the
exit mode and ask for help by running the
following
• :help
29. L2B Second Linux Course
L2B Linux course
Configuring vi and vim
:set
• :set number
• :set all
• :set number (:se nu)
• :set nonumber (:se nonu)
• :set ignorecase (:se ic)
• :set noignorecase (:se noic)
• :set showmatch (:se sm)
• :set noshowmatch(:se nosm)
• :set autoindent
• :set noautoindent
30. L2B Second Linux Course
L2B Linux course
Configuring vi and vim
:help
:help something
:set
• :set textwidth=3
• :set textwidth=0
• :set wrapmargin=10
• :set wrapmargin=0
~/.vimrc
vimtutor
31. L2B Second Linux Course
L2B Linux course
Basic System
Configuration Tools
32. L2B Second Linux Course
L2B Linux course
Introduction
Network interfaces are named sequentially:
eth0, eth1, etc
Multiple addresses can be assigned to a
device with aliases
Aliases are labeled eth0:1, eth0:2, etc.
Aliases are treated like separate interfaces
View interface configuration with :
ifconfig [ethX]
Enable interface with ifup ethX
Disable interface with ifdown ethX
33. L2B Second Linux Course
L2B Linux course
Graphical Network Configuration
system-config-network
System > Administration > Network
Network Configuration Files For Devices
/etc/sysconfig/network-scripts/ifcfg-ethX
Complete list of options in
/usr/share/doc/initscripts-*/sysconfig.txt
34. L2B Second Linux Course
L2B Linux course
Example Of Dynamic Configuration
DEVICE=ethX
HWADDR=0:02:8A:A6:30:45
BOOTPROTO=dhcp
ONBOOT=yes
Type=Ethernet
35. L2B Second Linux Course
L2B Linux course
Example Of Static Configuration
DEVICE=ethX
HWADDR=0:02:8A:A6:30:45
IPADDR=192.168.0.254
NETMASK=255.255.255.0
GATEWAY=192.168.2.254
ONBOOT=yes
Type=Ethernet
36. L2B Second Linux Course
L2B Linux course
Network Configuration Files and Other
Global Network Settings
/etc/sysconfig/network
• NETWORKING=yes
• HOSTNAME=server1.example.com
• GATEWAY=192.168.2.254
37. L2B Second Linux Course
L2B Linux course
DNS configuration
Server address is specified by dhcp or in
/etc/resolv.conf
• nameserver 192.168.0.254
• nameserver 192.168.1.254
38. L2B Second Linux Course
L2B Linux course
Setting the System's Date and Time
GUI:
• system-config-date
• System->Administration->Date & Time
CLI: date [MMDDhhmm[[CC]YY][.ss]]
• date 01011330
• Date 010113302007.05
• Date 12312359
• Date 123123592007
• Date 01010101.01
39. L2B Second Linux Course
L2B Linux course
Scripting
Taking input with positional parameters
• $1
• $2
• $3
• $4, etc.
• $*
• $#
40. L2B L2B Second Linux
Course Second Linux
L2B Linux course
Course
Scripting
Taking input with the read command
• read x
• read -p "Enter a filename: " file