Read LPI Linux Certification in a Nutshell Online
Authors: Adam Haeder; Stephen Addison Schneiter; Bruno Gomes Pessanha; James Stanger
Tags: #Reference:Computers
Anchors
Anchors are used to describe position information.
Table 6-8
, shown
earlier, lists anchor
characters
.
Display all lines from
file1
where the stringLinux
appears at
the start of the line:
$grep '^Linux' file1
Display lines in
file1
where the last
character is anx
:
$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 wordnull
by
itself:
$grep '^null$' file1
Groups and ranges
Characters can be placed into groups and ranges to
make regular expressions more efficient, as shown in
Table 6-10
, previously.
Display all lines from
file1
containingLinux
,linux
,TurboLinux
, and so on:
$grep '[Ll]inux' file1
Display all lines from
file1
that contain
three adjacent digits:
$grep '[0-9][0-9][0-9]' file1
Display all lines from
file1
beginning
with any single character other than a digit:
$grep '^[^0-9]' file1
Display all lines from
file1
that contain
the whole wordLinux
orlinux
, but notLinuxOS
orTurboLinux
:
$grep '\<[Ll]inux\>' file1
Display all lines from
file1
with five or
more characters on a line (excluding the newline
character
):
$grep '.....' file1
Display all nonblank lines from
file1
(i.e., that have at least one character):
$grep '.' file1
Display all lines from
file1
that contain
a period (normally a metacharacter) using an escape:
$grep '\.' file1
Modifiers
Modifiers change the meaning of other characters in a
regular expression.
Table 6-11
,
shown previously, lists these modifiers.
Display all lines from
file1
that containab
,abc
,abcc
,abccc
, and so on:
$grep 'abc*' file1
Display all lines from
file1
that containabc
,abcc
,abccc
, and so on, but notab
:
$grep 'abcc*' file1
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
Display lines from
file1
that containfile
(because?
can match zero occurrences),file1
, orfile2
:
$grep 'file[12]\?' file1
Display all lines from
file1
containing
at least one digit:
$grep '[0-9]\+' file1
Display all lines from
file1
that contain111
,1111
, or11111
on a line by itself:
$grep '^1\{3,5\}$' file1
Display all lines from
file1
that contain
any three-, four-, or five-digit number:
$grep '\<[0-9]\{3,5\}\>' file1
Display all lines from
file1
that containHappy
,happy
,Sad
,sad
,Angry
, orangry
:
$grep -E '[Hh]appy|[Ss]ad|[Aa]ngry' file1
Display all lines of
file
that contain
any repeated sequence ofabc
(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.
Basic regular expression patterns
Match any letter:
[A-Za-z]
Match any symbol (not a letter or digit):
[^0-9A-Za-z]
Match an uppercase letter, followed by zero or more lowercase
letters:
[A-Z][a-z]*
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\}
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\}
Match the month of June and its abbreviation,Jun
. The question mark matches zero or one
instance of thee
:
June\?
On the Exam
Make certain you are clear about the difference between
file globbing
and the use of
regular expressions.
Using regular expressions as addresses in sed
These examples are commands you would issue to
sed
. For example, the commands could take the
place ofcommand1
in this usage:
$sed
[options]
'command1
'[files]
These commands could also appear in a standalonesed
script.
Delete blank lines:
/^$/d
Delete any line that doesn’t contain#keepme
:
/#keepme/!d
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
Delete lines beginning with periods or pound signs:
/^[\.#]/d
Substitute a single space for any number of spaces wherever
they occur on the line:
s/ */ /g
or:
s/ \{2,\}/ /g
Substitutedef
forabc
from line 11 to 20, wherever it occurs
on the line:
11,20s/abc/def/g
Translate the characters
a
,
b
, and
c
to the
@
character from line 11 to 20, wherever they
occur on the line:
11,20y/abc/@@@/
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
.
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).
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, use10r
. Commonly used
vi
commands are listed in
Table 6-12
.
Table 6-12. vi commands
Key command | Description |
---|---|
| Move left one |
| Move down one line. |
| Move up one line. |
| Move right one |
| Move to the top of the |
| Move to the bottom of the |
| Move to the end of the |
| Move forward one word. |
| Move backward one word. |
| Move to the beginning of the current |
| Move to the first nonwhitespace |
| Move to the end of the current |
Ctrl-B | Move up (back) one |
Ctrl-F | Move down (forward) one |
| Insert at the current cursor |
| Insert at the beginning of the current |
| Append after the current cursor |
| Append to the end of the current |
| Start a new line after the current |
| Start a new line before the current |
| Replace the character at the current |
| Start replacing (overwriting) at the |
| Delete the character at the current |
| Delete the character immediately |
| Delete the character at the current |
| Delete the contents of the current |
| Given a movement command |
| Cut the entire current |
| Cut from the current cursor position |
| Given a movement command |
| Cut the entire current line and go |
| Cut from the current cursor position |
| Given a movement command |
| Copy the entire current |
| Paste after the current cursor |
| Paste before the current cursor |
| Repeat the last |
| Undo the last command. |
| Search forward for |
| Search backward for |
| Find the next match. |
| Find the previous match. (In other |
| Next file; when multiple files are |
| Load |
| Insert the contents of |
| Quit without saving changes. Force |
| Write the current buffer to |
| Write the file contents and quit. |
| Write the file contents (if changed) |
| Write the file contents (if changed) |
| Execute |
[ [ |
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
andI
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.