shell
Matching Single Characters
* 匹配0个或者多个任意字符
? 匹配单个任意字符
[] 匹配范围
[0-9]数字
[a-z]小写字母
[!a-z]非小写字母
标准输入 可以来自文件或者输入
标准输出 可以输出到文件或者输出
重定向标准错误
command 2> file
$ ls a* 2> errors
$ cat errors
/bin/ls: a*: No such file or directory
多个命令执行使用;隔开+
$ date; pwd
Fri Nov 11 11:29:55 CST 2011
/root/bhz
后台执行&
ln file1 file2
Link file1 to file2
ln file(s) dir
正则表达式
. 匹配任意单个字符
/ ... /查找被两个空格包围的三个字符
The Unix operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the Unix system was to create an
environment that promoted efficient program
development.
$ ed intro
255
$s/p.o/XXX/g Change all p.os to XXX
^匹配行首
$匹配行尾
行末加>>
sed 's/$/>>/' intro
删除每行的最后的两个字符
sed 's/..$//' intro
^$匹配空行
[]选择
[tT]he
[0123456789]
[0-9]
[A-Z]
*
X*
匹配出现次数0次或者任意次
e.*e
匹配第一个e和最后一个e之间
[A-Za-z][A-Za-z]*
匹配所有单词
[-0-9]
匹配-或一个数字
[]a-z]
匹配]或一个字母
精确匹配
\{...\}
\{min,max\}
X\{1,10\}
sed -n '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/p' file
192.168.1.123
grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" file
192.168.1.123
匹配a 10 次
a\{10\}
保存匹配的字符
\(...\)
It is possible to capture the characters matched within a regular expression by
enclosing the characters inside backslashed parentheses. These captured characters
are stored in "registers" numbered 1 through 9.
phone
Alice Chebba 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony Iannino 973-555-1295
sed -n 's/\(.*\) \(.*\)/\2 \1/p' phone
973-555-2015 Alice Chebba
212-555-2298 Liz Stachiw
201-555-7776 Susan Goldberg
973-555-1295 Tony Iannino
阅读(1016) | 评论(0) | 转发(0) |