GNU/Linux文本处理和进程管理命令
时间:2006-09-17 来源:linxh
(1) 文本处理过滤器
cut - remove sections from each line of files
Syntax: cut [OPTION]... [FILE]...
Options:
-b, --bytes=LIST
output only these bytes
-c, --characters=LIST
output only these characters
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-f, --fields=LIST
output only these fields; also print any line that contains no delimiter character, unless the -s option is specified
-s, --only-delimited
do not print lines not containing delimiters
Examples:
$ cut -d: -f1 /etc/passwd
$ cut -c 1 /etc/passwd
expand - convert tabs to spaces
Syntax: expand [OPTION]... [FILE]...
Options:
-t, --tabs=NUMBER
have tabs NUMBER characters apart, not 8
-i, --initial
do not convert TABs after non whitespacev
fmt - simple optimal text formatter
Syntax: fmt [options] [files]
Options:
-u, --uniform-spacing
one space between words, two after sentences
-w, --width=WIDTH
maximum line width (default of 75 columns)
head - output the first part of files
Syntax: head [OPTION]... [FILE]...
Options:
-c, --bytes=[-]N
print the first N bytes of each file; with the leading '-', print all but the last N bytes of each file
-n, --lines=[-]N
print the first N lines instead of the first 10; with the lead-ing '-', print all but the last N lines of each file
join - join lines of two files on a common field
Syntax: join [OPTION]... FILE1 FILE2
Options:
-j FIELD
equivalent to '-1 FIELD -2 FIELD'
-1 FIELD
join on this FIELD of file 1
-2 FIELD
join on this FIELD of file 2
Examples:
$ join -j 1 file1 file2
nl - number lines of files
Syntax: nl [OPTION]... [FILE]...
Options:
-b, --body-numbering=STYLE
use STYLE for numbering body lines
-f, --footer-numbering=STYLE
use STYLE for numbering footer lines
-h, --header-numbering=STYLE
use STYLE for numbering header lines
STYLE is one of:
a number all lines
t number only nonempty lines
n number no lines
pREGEXP
number only lines that contain a match for REGEXP
Examples:
$ nl -h a file1
od - dump files in octal and other formats
Syntax:
od [OPTION]... [FILE]...
od --traditional [FILE] [[+]OFFSET [[+]LABEL]]
Options:
-t, --format=TYPE
select output format or formats
TYPE is made up of one or more of these specifications:
a named character
c ASCII character or backslash escape
d[SIZE]
signed decimal, SIZE bytes per integer
f[SIZE]
floating point, SIZE bytes per integer
o[SIZE]
octal, SIZE bytes per integer
u[SIZE]
unsigned decimal, SIZE bytes per integer
x[SIZE]
hexadecimal, SIZE bytes per integer
Examples:
$ od -t a file1
$ od -t c file1
$ od -t x1 file1
$ od -t o1 file1
paste - merge lines of files
Syntax: paste [OPTION]... [FILE]...
Options:
-d, --delimiters=LIST
reuse characters from LIST instead of TABs
-s, --serial
paste one file at a time instead of in parallel
Examples:
$ paste file1 file2
$ paste -d'@' file1 file2
$ paste -s file1 file2
pr - convert text files for printing
Syntax: pr [OPTION]... [FILE]...
Options:
-d, --double-space
double space the output
-h HEADER, --header=HEADER
use a centered HEADER instead of filename in page header, -h "" prints a blank line, don't use -h""
-l PAGE_LENGTH, --length=PAGE_LENGTH
set the page length to PAGE_LENGTH (66) lines (default number of lines of text 56, and with -F 63)
-o MARGIN, --indent=MARGIN
offset each line with MARGIN (zero) spaces, do not affect -w or -W, MARGIN will be added to PAGE_WIDTH
split - split a file into pieces
Syntax: split [OPTION] [INPUT [PREFIX]]
Options:
-b, --bytes=SIZE
put SIZE bytes per output file
Examples:
$ split -2 file1 splitout_
tac - concatenate and print files in reverse
Syntax: tac [OPTION]... [FILE]...
Examples:
$ tac file1
tail - output the last part of files
Syntax: tail [OPTION]... [FILE]...
Options:
-c, --bytes=N
output the last N bytes
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent
-n, --lines=N
output the last N lines, instead of the last 10
tr - translate or delete characters
Syntax: tr [OPTION]... SET1 [SET2]
Options:
-d, --delete
delete characters in SET1, do not translate
-s, --squeeze-repeats
replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
Examples:
$ cat file1 | tr a-z A-Z
$ tr a-z A-Z < file1
$ cat file1 | tr -s a
$ cat file1 | tr -d abc
wc - print the number of newlines, words, and bytes in files
Syntax: wc [OPTION]... [FILE]...
Options:
-c, --bytes
print the byte counts
-l, --lines
print the newline counts
-w, --words
print the word counts
Examples:
$ wc file[123]
$ wc -l file1
xargs - build and execute command lines from standard input
Syntax: xargs [-0prtx] [-E eof-str] [-e[eof-str]] [-I replace-str]
[-i[replace-str]] [-L max-lines] [-l[max-lines]] [-n max-args] [-s
max-chars] [-P max-procs] [--null] [--eof[=eof-str]]
[--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]
[--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]
[--max-args=max-args] [--no-run-if-empty] [--version] [--help] [com-
mand [initial-arguments]]
Options:
--max-args=max-args, -n max-args
Use at most max-lines nonblank input lines per command line; max-lines defaults to 1 if omitted. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x.
--interactive, -p
Prompt the user about whether to run each command line and read a line from the terminal. Only run the command line if the response starts with 'y' or 'Y'. Implies -t.
Examples:
$ find / -type f | xargs -n 1 grep linux
(2) 流编辑器sed
sed - sed is a stream editor
Syntax:
sed [OPTION]... 'command1' [FILE]...
sed [OPTION]... -e 'command1' [-e 'command2' ...] [FILE]...
sed [OPTION]... -f script-file [FILE]...
Options:
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
Text addressing:
- A line number,such as: 102,125
- A line number with interval, in form of n~s, n indicating starting of line number, and s indicating step, every s step
- Symbol $ indicating the last line of the last input file
- A regular expression delimited by forward slashes(/regex/)
Commands which accept address ranges:
d Delete pattern space. Start next cycle.
s/regexp/replacement/ Attempt to match regexp against the pattern space. Flags used with s command:g, n, p, w file, y
Examples:
$ sed '3,5d' file1
$ sed '/^#/d' file1
y/abc/xyz/
$ sed 's/^$/@/' file1
$ sed 's/"//g' file1
$ sed -f sedcmds file1
(3) 使用tee
tee - read from standard input and write to standard output and files
Syntax: tee [OPTION]... [FILE]...
Options:
-a, --append
append to the given FILEs, do not overwrite
Examples:
$ cmd1 | tee file_cmd1 | cmd2 | cmd3 > file1
(4) 进程和作业管理
ps - report a snapshot of the current processes
Syntax: ps [options]
Options:
-a
Select all processes except session leaders (see getsid(2)) and processes not associated with a terminal.
-f
does full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added.
-l
long format. The -y option is often useful with this.
-u userlist
Select by effective user ID (EUID) or name.
-w
Wide output. Use this option twice for unlimited width.
x
processes w/o controlling ttys
-C cmdlist
Select by command name.
-U usrlist
select by real user ID (RUID) or name.
Examples:
$ ps
$ ps -aux
$ ps aux
$ ps u -C httpd
pstree - display a tree of processes
Syntax: pstree [-a] [-c] [-h|-Hpid] [-l] [-n] [-p] [-u] [-Z] [-G|-U] [pid|user]
pstree -V
Options:
-a Show command line arguments. If the command line of a process is swapped out, that process is shown in parentheses. -a implicitly disables compaction.
-c Disable compaction of identical subtrees. By default, subtrees are compacted whenever possible.
-G Use VT100 line drawing characters.
-h Highlight the current process and its ancestors. This is a no-op if the terminal doesn't support highlighting or if neither the current process nor any of its ancestors are in the subtree being shown.
-n Sort processes with the same ancestor by PID instead of by name. (Numeric sort.)
-p Show PIDs. PIDs are shown as decimal numbers in parentheses after each process name. -p implicitly disables compaction.
Examples:
$ pstree -p
top - display Linux tasks
Syntax: top -hv | -bcisS -d delay -n iterations -p pid [, pid ...]
Options:
-b : Batch mode operation
Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.
-d : Delay time interval as: -d ss.tt (seconds.tenths)
Specifies the delay between screen updates, and overrides the corresponding value in one's personal configuration file or the startup default. Later this can be changed with the 'd' or 's' interactive commands.
Fractional seconds are honored, but a negative number is not allowed. In all cases, however, such changes are prohibited if top is running in 'Secure mode', except for root (unless the 's' command-line option was used). For additional information on 'Secure mode' see topic 5a. SYSTEM Configuration File.
-i : Idle Processes toggle
Starts top with the last remembered 'i' state reversed. When this toggle is Off, tasks that are idled or zombied will not be displayed.
-n : Number of iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top should produce before ending.
-s : Secure mode operation
Starts top with secure mode forced, even for root. This mode is far better controlled through the system configuration file
Examples:
$ top
$ top -d 1
$ top -qi
$ top bin 5 d 1 > file1
kill - terminate a process
Syntax:
kill [ -s signal | -p ] [ -a ] [ -- ] pid ...
kill -l [ signal ]
Examples:
$ kill -l 15
$ kill 1000 1001
$ kill -15 1000 1001
$ kill -SIGTERM 1000 1001
$ kill -sigterm 1000 1001
$ kill -TERM 1000 1001
$ kill -s 15 1000 1001
$ kill -s SIGTERM 1000 1001
$ kill -HUP `cat /var/run/inetd.pid`
bg
Syntax: bg [jobspec]
fg
Syntax: fg [jobspec]
jobs
Syntax: jobs [-lnprs] [jobspec ...] or jobs -x command [args]
nice - run a program with modified scheduling priority
Syntax: nice [OPTION] [COMMAND [ARG]...]
Options:
-n, --adjustment=ADJUST
increment priority by ADJUST first
Examples:
$ nice somecmd -opt1 -opt2 arg1 arg2 &
$ nice --10 vi /etc/hosts.deny
$ nice -n -10 vi /etc/hosts.allow
renice - alter priority of running processes
Syntax: renice priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]
Options:
-u Force the who parameters to be interpreted as user names.
-p Resets the who interpretation to be (the default) process ID's.
Examples:
$ renice 20 501
$ renice -10 -u jdean -p 501
(5) grep与正则表达式
Syntax:
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]
Options:
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines.
-h, --no-filename
Suppress the prefixing of filenames on output when multiple files are searched.
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files.
-n, --line-number
Prefix each line of output with the line number within its input file.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
Examples:
$ grep -i linux file1
$ grep 'abc*' abc*
$ grep "abc*" abc*
$ grep abc\* abc*
$ grep '^Linux' file1
$ grep 'x$' file1
$ grep -c '^$' file1
$ grep '^null$' file1
$ grep '[Ll]inux' file1
$ grep '[0-9][0-9]' file1
$ grep '^[^0-9]' file1
$ grep '\<[Ll]inux\>' file1
$ grep '.....' file1
$ grep '\.' file1
参考资料:
O'Reilly, LPI Linux Certification In A Nutshell
Red Hat Enterprise Linux 4 手册