LPI Linux Certification in a Nutshell (14 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
6.33Mb size Format: txt, pdf, ePub
Name

nl

Syntax
nl [
options
] [
files
]
Description

Number the lines of
files
,
which are concatenated in the output. This command is used for
numbering lines in the body of text, including special header and
footer options normally excluded from the line numbering. The
numbering is done for each
logical page
, which is defined as
having a header, a body, and a footer. These are delimited by the
special strings
\:\:\:
,
\:\:
, and
\:
, respectively.

Frequently used options
-b
style

Set body numbering style to
style
, which is
t
by default (styles are described
next).

-f
style

Set footer number style to
style
(
n
by default).

-h
style

Set header numbering style to
style
, (
n
by default).

Styles can be in these forms:

A

Number all lines.

t

Number only nonempty lines.

n

Do not number lines.

p
REGEXP

Number only lines that contain a match for regular
expression
REGEXP
.

Example

Suppose file
file1
contains the following
text:

\:\:\:
header
\:\:
line1
line2
line3
\:
footer
\:\:\:
header
\:\:
line1
line2
line3
\:
footer

If the following command is given:

$
nl -h a file1

the output would yield numbered headers and body lines but no
numbering on footer lines. Each new header represents the beginning of
a new logical page and thus a restart of the numbering
sequence:

1  header
2 line1
3 line2
4 line3
footer
1 header
2 line1
3 line2
4 line3
footer
Name

od

Syntax
od [
options
] [
files
]
Description

Dump files in octal and other formats. This program
prints a listing of a file’s contents in a variety of formats. It is
often used to examine the byte codes of binary files but can be used
on any file or input stream. Each line of output consists of an octal
byte offset from the start of the file followed by a series of tokens
indicating the contents of the file. Depending on the options
specified, these tokens can be ASCII, decimal, hexadecimal, or octal
representations of the contents.

Frequently used options
-t
type

Specify the
type
of
output.

Typical types include:

A

Named character

c

ASCII character or backslash escape

O

Octal (the default)

x

Hexadecimal

Example

If
file1
contains:

a1\n
A1\n

where
\n
stands for the
newline character, the
od
command specifying
named characters yields the following output:

$
od -t a file1
00000000 a 1 nl A 1 nl
00000006

A slight nuance is the ASCII character mode. This
od
command specifying named characters yields the
following output with backslash-escaped characters rather than named
characters:

$
od -t c file1
00000000 a 1 \n A 1 \n
00000006

With numeric output formats, you can instruct
od
on how many bytes to use in interpreting each
number in the data. To do this, follow the type specification by a
decimal integer. This
od
command specifying
single-byte hex results yields the following output:

$
od -t x1 file1
00000000 61 31 0a 41 31 0a
00000006

Doing the same thing in octal notation yields:

$
od -t o1 file1
00000000 141 061 012 101 061 012
00000006

If you examine an ASCII chart with hex and octal
representations, you’ll see that these results match those
tables.

Name

paste

Syntax
paste [
options
] [
files
]
Description

Paste together corresponding lines of one or more
files
into vertical columns. Similar in
function to the
join
command, but simpler in
scope.

Frequently used options
-d
n

Separate columns with character
n
in place of the default Tab.

-s

Merge lines from one file into a single line. When
multiple files are specified, their contents are placed on
individual lines of output, one per file.

For the following three examples,
file1
contains:

1
2
3

and
file2
contains:

A
B
C
Example 1

A simple
paste
creates columns from each
file in standard output:

$
paste file1 file2
1 A
2 B
3 C
Example 2

The column separator option yields columns separated by the
specified character:

$
paste -d'@' file1 file2
1@A
2@B
3@C
Example 3

The single-line option (
-s
) yields a line
for each file:

$
paste -s file1 file2
1 2 3
A B C
Name

pr

Syntax
pr [
options
] [
file
]
Description

Convert a text file into a paginated, columnar version,
with headers and page fills. This command is convenient for yielding
nice output, such as for a line printer from raw, uninteresting text
files. The header will consist of the date and time, the filename, and
a page number.

Frequently used options
-d

Double space.

-h
header

Use
header
in place of the
filename in the header.

-l
lines

Set page length to
lines
. The
default is 66.

-o
width

Set the left margin to
width
.

Name

sort

Syntax
sort [
options
] [
files
]
Description

Write input to
stdout
(standard
out), sorted alphabetically.

Frequently used options
-f

Case-insensitive sort.

-k
POS1
[,
POS2
]

Sort on the key starting at
POS1
and (optionally) ending at
POS2
.

-n

Sort numerically.

-r

Sort in reverse order.

-t
SEP

Use
SEP
as the key separator.
The default is to use whitespace as the key separator.

Example

Sort all processes on the system by resident size (
RSS
in
ps
):

$
ps aux | sort -k 6 -n
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? SW Feb08 0:00 [keventd]
root 3 0.0 0.0 0 0 ? SWN Feb08 0:00 [ksoftirqd_CPU0]
root 4 0.0 0.0 0 0 ? SW Feb08 0:01 [kswapd]
root 5 0.0 0.0 0 0 ? SW Feb08 0:00 [bdflush]
root 6 0.0 0.0 0 0 ? SW Feb08 0:00 [kupdated]
root 7 0.0 0.0 0 0 ? SW Feb08 0:00 [kjournald]
root 520 0.0 0.3 1340 392 tty0 S Feb08 0:00 /sbin/mingetty tt
root 335 0.0 0.3 1360 436 ? S Feb08 0:00 klogd -x
root 1 0.0 0.3 1372 480 ? S Feb08 0:18 init
daemon 468 0.0 0.3 1404 492 ? S Feb08 0:00 /usr/sbin/atd
root 330 0.0 0.4 1424 560 ? S Feb08 0:01 syslogd -m 0
root 454 0.0 0.4 1540 600 ? S Feb08 0:01 crond
root 3130 0.0 0.5 2584 664 pts/0 R 13:24 0:00 ps aux
root 402 0.0 0.6 2096 856 ? S Feb08 0:00 xinetd -stayalive
root 385 0.0 0.9 2624 1244 ? S Feb08 0:00 /usr/sbin/sshd
root 530 0.0 0.9 2248 1244 pts/0 S Feb08 0:01 -bash
root 3131 0.0 0.9 2248 1244 pts/0 R 13:24 0:00 -bash
root 420 0.0 1.3 4620 1648 ? S Feb08 0:51 sendmail: accepti
root 529 0.0 1.5 3624 1976 ? S Feb08 0:06 /usr/sbin/sshd
Name

split

Syntax
split [
option
] [
infile
] [
outfile
]
Description

Split
infile
into a specified
number of line groups, with output going into a succession of files,
outfile
aa
,
outfile
ab
, and so on (the default is
xaa
,
xab
, etc.). The
infile
remains unchanged. This command is
handy if you have a very long text file that needs to be reduced to a
succession of smaller files. This was often done to email large files
in smaller chunks, because at one time it was considered bad practice
to a send a single large email message.

Frequently used option
-n

Split the
infile
into
n
-line segments. The default is
1,000.

Example

Suppose
file1
contains:

1  one
2 two
3 three
4 four
5 five
6 six

Then the command:

$
split -2 file1 splitout_

yields as output three new files,
splitout_aa
,
splitout_ab
,
and
splitout_ac
.

The file
splitout_aa
contains:

1  one
2 two

splitout_ab
contains:

3  three
4 four

and
splitout_ac
contains:

5  five
6 six
Name

tac

Syntax
tac [
file
]
Description

This command is named as an opposite for the
cat
command, which simply prints text files to
standard output. In this case,
tac
prints the
text files to standard output with lines in reverse order.

Example

Suppose
file1
contains:

1  one
2 two
3 three

Then the command:

$
tac file1

yields as output:

3  three
2 two
1 one
Name

tail

Syntax
tail [
options
] [
files
]
Description

Print the last few lines of one or more
files
(the “tail” of the file or files).
When more than one file is specified, a header is printed at the
beginning of each file, and each is listed in succession.

Frequently used options
-c
n

This option prints the last
n
bytes, or if
n
is followed by
k
or
m
, the last
n
kilobytes or megabytes,
respectively.

-n
m

Prints the last
m
lines. The
default is 10.

-f

Continuously display a file as it is actively written by
another process (“follow” the file). This is useful for watching
logfiles as the system runs.

Other books

Unknown by Unknown
Dark Angel by Maguire, Eden
The Speckled Monster by Jennifer Lee Carrell
Soft Apocalypse by Will McIntosh
Why Do I Have to Think Like a Man? by Shanae Hall, Rhonda Frost
Queen of Diamonds by Cox, Sandra
MORTAL COILS by Unknown
Cages by Peg Kehret