LPI Linux Certification in a Nutshell (23 page)

Read LPI Linux Certification in a Nutshell Online

Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger

Tags: #Reference:Computers

BOOK: LPI Linux Certification in a Nutshell
7.68Mb size Format: txt, pdf, ePub
Name

Anchors

Description

Anchors are used to describe position information.
Table 6-8
, shown
earlier, lists anchor
characters
.

Example 1

Display all lines from
file1
where the string
Linux
appears at
the start of the line:

$
grep '^Linux' file1
Example 2

Display lines in
file1
where the last
character is an
x
:

$
grep 'x$' file1

Display the number of empty lines in
file1
by finding lines with nothing between the
beginning and the end:

$
grep -c '^$' file1

Display all lines from
file1
containing
only the word
null
by
itself:

$
grep '^null$' file1
Name

Groups and ranges

Description

Characters can be placed into groups and ranges to
make regular expressions more efficient, as shown in
Table 6-10
, previously.

Example 1

Display all lines from
file1
containing
Linux
,
linux
,
TurboLinux
, and so on:

$
grep '[Ll]inux' file1
Example 2

Display all lines from
file1
that contain
three adjacent digits:

$
grep '[0-9][0-9][0-9]' file1
Example 3

Display all lines from
file1
beginning
with any single character other than a digit:

$
grep '^[^0-9]' file1
Example 4

Display all lines from
file1
that contain
the whole word
Linux
or
linux
, but not
LinuxOS
or
TurboLinux
:

$
grep '\<[Ll]inux\>' file1
Example 5

Display all lines from
file1
with five or
more characters on a line (excluding the newline
character
):

$
grep '.....' file1
Example 6

Display all nonblank lines from
file1
(i.e., that have at least one character):

$
grep '.' file1
Example 7

Display all lines from
file1
that contain
a period (normally a metacharacter) using an escape:

$
grep '\.' file1
Name

Modifiers

Description

Modifiers change the meaning of other characters in a
regular expression.
Table 6-11
,
shown previously, lists these modifiers.

Example 1

Display all lines from
file1
that contain
ab
,
abc
,
abcc
,
abccc
, and so on:

$
grep 'abc*' file1
Example 2

Display all lines from
file1
that contain
abc
,
abcc
,
abccc
, and so on, but not
ab
:

$
grep 'abcc*' file1
Example 3

Display all lines from
file1
that contain
two or more adjacent digits:

$
grep '[0-9][0-9][0-9]*' file1

or:

$
grep '[0-9]\{2,\}' file1
Example 4

Display lines from
file1
that contain
file
(because
?
can match zero occurrences),
file1
, or
file2
:

$
grep 'file[12]\?' file1
Example 5

Display all lines from
file1
containing
at least one digit:

$
grep '[0-9]\+' file1
Example 6

Display all lines from
file1
that contain
111
,
1111
, or
11111
on a line by itself:

$
grep '^1\{3,5\}$' file1
Example 7

Display all lines from
file1
that contain
any three-, four-, or five-digit number:

$
grep '\<[0-9]\{3,5\}\>' file1
Example 8

Display all lines from
file1
that contain
Happy
,
happy
,
Sad
,
sad
,
Angry
, or
angry
:

$
grep -E '[Hh]appy|[Ss]ad|[Aa]ngry' file1
Example 9

Display all lines of
file
that contain
any repeated sequence of
abc
(
abcabc
,
abcabcabc
, and so on):

$
grep '\(abc\)\{2,\}' file

You may find it useful to employ the GNU option
--color
to
grep
when
working with regular expressions. It prints the section of the
string that matched your regular expression in a different color, so
you can see exactly what
grep
was looking
for.

Name

Basic regular expression patterns

Example 1

Match any letter:

[A-Za-z]
Example 2

Match any symbol (not a letter or digit):

[^0-9A-Za-z]
Example 3

Match an uppercase letter, followed by zero or more lowercase
letters:

[A-Z][a-z]*
Example 4

Match a U.S. Social Security Number (123-45-6789) by
specifying groups of three, two, and four digits separated by
dashes:

[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}
Example 5

Match a dollar amount, using an escaped dollar sign, zero or
more spaces or digits, an escaped period, and two more
digits:

\$[ 0-9]*\.[0-9]\{2\}
Example 6

Match the month of June and its abbreviation,
Jun
. The question mark matches zero or one
instance of the
e
:

June\?

On the Exam

Make certain you are clear about the difference between
file globbing
and the use of
regular expressions.

Name

Using regular expressions as addresses in sed

These examples are commands you would issue to
sed
. For example, the commands could take the
place of
command1
in this usage:

$
sed
[options]
'
command1
'
[files]

These commands could also appear in a standalone
sed
script.

Example 1

Delete blank lines:

/^$/d
Example 2

Delete any line that doesn’t contain
#keepme
:

/#keepme/!d
Example 3

Delete lines containing only whitespace (spaces or Tabs). In
this example,
Tab
means the
single Tab character and is preceded by a single space:

/^[ Tab]*$/d

Because GNU
sed
also supports character
classes, this example could be written as follows:

/^[[:blank:]]*$/d
Example 4

Delete lines beginning with periods or pound signs:

/^[\.#]/d
Example 5

Substitute a single space for any number of spaces wherever
they occur on the line:

s/  */ /g

or:

s/ \{2,\}/ /g
Example 6

Substitute
def
for
abc
from line 11 to 20, wherever it occurs
on the line:

11,20s/abc/def/g
Example 7

Translate the characters
a
,
b
, and
c
to the
@
character from line 11 to 20, wherever they
occur on the line:

11,20y/abc/@@@/
Objective 8: Perform Basic File Editing Operations Using vi

vi
is perhaps the most ubiquitous text editor
available on Linux systems. Since most system administration tasks
eventually require editing text files, being able to work effectively in
vi
is essential.

This Objective concentrates on a subset of
vi
functionality.
Learning the vi and Vim
Editors
(O’Reilly) is an indispensable reference for
anyone interested in learning more about
vi
and the
enhancements available in its various implementations. There is also a
large amount of documentation available at
http://vimdoc.sourceforge.net
and
http://www.vim.org
for the popular
vi
implementation Vim, most of which is applicable to
any version of
vi
.

Invoking vi

To start
vi
, simply execute it. You will be
editing a temporary file. To directly edit one or more files, give the
names of the files on the command line:

$
vi file1.txt file2.txt

You are presented with a main window showing the contents of
file1.txt
, or if the specified files don’t already
exist, a blank screen with
tilde (
~
) characters
running the length of the left column (they indicate areas of the screen
containing no text, not even blank lines).

vi Basics

The
vi
editor has two modes of operation:
command
or
insert
. In command
mode,
vi
allows you to navigate around your file
and enter commands. To enter new text, put
vi
into
insert mode. In command mode, the keyboard keys are interpreted as
vi
commands instead of text. The convenience of
being able to manipulate the editor without moving your hands from the
keyboard is considered one of
vi
’s
strengths.

Commands are brief, case-sensitive combinations of one or more
letters. For example, to switch from command to insert mode, press the
“i” key. To terminate insert mode, press the Escape key (Esc), which
puts you back in command mode.

Almost any command can be prefixed with a number to repeat the
command that number of times. For example,
r
will replace the character at the current
cursor position. To replace exactly 10 characters, use
10r
. Commonly used
vi
commands are listed in
Table 6-12
.

Table 6-12. vi commands

Key command

Description

h
or left arrow

Move left one
character.

j
or down arrow

Move down one line.

k
or up arrow

Move up one line.

l
or right arrow

Move right one
character.

H

Move to the top of the
screen.

L

Move to the bottom of the
screen.

G

Move to the end of the
file.

w

Move forward one word.

b

Move backward one word.

0
(zero)

Move to the beginning of the current
line.

^

Move to the first nonwhitespace
character on the current line.

$

Move to the end of the current
line.

Ctrl-B

Move up (back) one
screen.

Ctrl-F

Move down (forward) one
screen.

i

Insert at the current cursor
position.

I

Insert at the beginning of the current
line.

a

Append after the current cursor
position.

A

Append to the end of the current
line.

o

Start a new line after the current
line.

O

Start a new line before the current
line.

r

Replace the character at the current
cursor position.

R

Start replacing (overwriting) at the
current cursor position.

x

Delete the character at the current
cursor position.

X

Delete the character immediately
before (to the left) of the current cursor
position.

s

Delete the character at the current
cursor position and go into insert mode. (This is the equivalent
of the combination
xi
.)

S

Delete the contents of the current
line and go into insert mode.

d
X

Given a movement command
X
, cut (delete) the appropriate
number of characters, words, or lines from the current cursor
position.

dd

Cut the entire current
line.

D

Cut from the current cursor position
to the end of the line. (This is equivalent to
d$
.)

c
X

Given a movement command
X
, cut the appropriate number of
characters, words, or lines from the current cursor position and
go into insert mode.

cc

Cut the entire current line and go
into insert mode.

C

Cut from the current cursor position
to the end of the line and enter insert mode. (This is
equivalent to
c$
.)

y
X

Given a movement command
X
, copy (yank
[
a
]
) the appropriate number of characters, words, or
lines from the current cursor position.

yy
or
Y

Copy the entire current
line.

p

Paste after the current cursor
position.

P

Paste before the current cursor
position.

.

Repeat the last
command.

u

Undo the last command.
[
b
]

/
regex

Search forward for
regex
.

?
regex

Search backward for
regex
.

n

Find the next match.

N

Find the previous match. (In other
words, repeat the last search in the opposite
direction.)

:n

Next file; when multiple files are
specified for editing, this command loads the next file. Force
this action (if the current file has unsaved changes) with
:n!
.

:e
file

Load
file
in place of the current file. Force this action with
:e!
file
.

:r
file

Insert the contents of
file
after the current cursor
position.

:q

Quit without saving changes. Force
this action with
:q!
.

:w
file

Write the current buffer to
file
. To append to an existing file,
use
:w
>>
file
. Force the
write (when possible, such as when running as
root
) with
:w!
file
.

:wq

Write the file contents and quit.
Force this action with
:wq!
.

:x

Write the file contents (if changed)
and quit (the
ex
equivalent
of
ZZ
).

ZZ

Write the file contents (if changed)
and quit.

:!
command

Execute
command
in a subshell.

[
a
]
Emacs users should be careful not to confuse the
vi
definition of yank (copy) with that
of Emacs (paste).

[
b
]
Many of the popular
vi
implementations support multilevel undo. Vim breaks
compatibility with traditional
vi
by
making a repeated
u
perform another level of undo. Nvi uses
.
after
u
to do multilevel undo and, like
traditional
vi
, uses a repeated
u
to redo (undo the undo,
so to speak). This can cause some confusion when moving
between Linux distributions that have different default
implementations of
vi
.

Note

Keep in mind that this is
not
a complete
list, but it is not necessary to know every
vi
command to use it effectively. In fact, even after using
vi
as your only editor for years, you may find
yourself using only a small subset of the available commands.

There is a pattern in
vi
’s keyboard commands
that makes them easier to remember. For every lowercase character that
has some action assigned to it, the same uppercase character
usually
has some related action assigned to it. As
an example,
i
and
I
both put
vi
in insert
mode, at the current cursor position and at the beginning of the line,
respectively.

On the Exam

You’ll need to be familiar with
vi
’s
command and insert modes, how to switch between them, and how to
perform basic navigation and editing tasks.

Other books

Blood Ties by Ralph McInerny
The Long Fall by Julia Crouch
PsyCop 2.2: Many Happy Returns by Jordan Castillo Price
Brodie's Gamble by Shirleen Davies
Judgment Day by Penelope Lively
Skull and Bones by John Drake
Smashwords version Sweet Surrender by Georgette St. Clair
Dead Ringer by Jessie Rosen