cut+paste
|
System V includes a nifty command called cut
that lets you
select a list of columns or fields from one or more files.
We've also included a public-domain version on the disc, for those of
you whose systems do without. |
You must specify either the -c
option to cut by column or
-f
to cut by fields.
(Fields are separated by tabs
unless you specify a different field separator
with -d
.
Use
quotes (8.14
)
if you want a space or other special
character as the delimiter.)
The column(s) or field(s) to cut must follow the option immediately,
without any space.
Use a comma between separate values
and a hyphen to specify a range (e.g., 1-10
, 15
, 20
, or 50-
).
cut
is incredibly handy.
Here are some examples:
Find out who is logged in, but list only login names:
who
|
% who | cut -d" " -f1
|
Extract usernames and real names from
/etc/passwd
(36.3
)
:
% cut -d: -f1,5 /etc/passwd
Cut characters in the fourth column of file
, and paste them
back as the first column in the same file:
paste
|
% cut -c4 file
| paste - file
|