重新复习正则
时间:2007-09-18 来源:codfei
\(.*\)("匹配所有字符直到行末")
[root@codfei bin]# echo 'foo(10,7,2)'|sed 's/foo(\([^,]*\),\([^,]*\),\([^)]*\
))/foo(\2,\1,\3)/g'
foo(7,10,2) [root@codfei bin]# sed 's/\(really \)\(really \)*/very /' testfile9
Billy tried very hard
Sally tried very hard
Timmy tried very hard
Johnny tried very hard
[root@codfei bin]# cat testfile9
Billy tried really hard
Sally tried really really hard
Timmy tried really really really hard
Johnny tried really really really really hard
sed 's/^$/d' price.txt 删除所有空行
sed 's/^[ \t]*$/d' price.txt 删除所有只包含空格或者制表符的行
sed 's/"//g' price.txt 删除所有引号
awk '$0 !~ /^$/' price.txt
# 删除所有空行
awk 'NF > 0' price.txt
# awk中一个更好的删除所有行的办法
awk '$2 ~ /^[JT]/ {print $3}' price.txt
# 打印所有第二个字段是'J'或者'T'打头的行中的第三个字段
awk '$2 !~ /[Mm]isc/ {print $3 + $4}' price.txt
# 针对所有第二个字段不包含'Misc'或者'misc'的行,打印第3和第4列的和(假定为数字)
awk '$3 !~ /^[0-9]+\.[0-9]*$/ {print $0}' price.txt
# 打印所有第三个字段不是数字的行,这里数字是指d.d或者d这样的形式,其中d是0到9的任何数字
awk '$2 ~ /John|Fred/ {print $0}' price.txt
# 如果第二个字段包含'John'或者'Fred'则打印整行 [root@codfei bin]# cat testfile10
Francis, John 5-3871
Wong, Fred 4-4123
Jones, Thomas 1-4122
Salazar, Richard 5-2522 [root@codfei bin]# grep '^S[^ ]* R' testfile10
Salazar, Richard 5-2522
[root@codfei bin]#
[root@codfei bin]#
[root@codfei bin]#
[root@codfei bin]# grep '^[JW]' testfile10
Wong, Fred 4-4123
Jones, Thomas 1-4122 [root@codfei bin]# grep -v '^[JW]' testfile10
Francis, John 5-3871
Salazar, Richard 5-2522
[root@codfei bin]# grep '^[M-Z]' testfile10
Wong, Fred 4-4123
Salazar, Richard 5-2522
#egrep 'net(work)?s' report.txt
#从report.txt中找到所有包含networks或者nets的行
[root@codfei bin]# egrep '(John|Fred)' testfile10
Francis, John 5-3871
Wong, Fred 4-4123
[root@codfei bin]# egrep 'John|22$|^W' testfile10
Francis, John 5-3871
Wong, Fred 4-4123
Salazar, Richard 5-2522
相关阅读 更多 +