Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26188266
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: LINUX

2009-03-12 17:17:32

1.一个比较实用的正则表达式
匹配html的嵌入代码 
<[^>]*>


匹配[....]的嵌入码
 \[[^]]\{1,\}\]


删除仅由空字符组成的行
sed '/^[[:space:]]*$/d' filename


匹配html标签
/\(<[^>]*>\)/
例如:从html文件中剔除html标签
sed 's/\(<[^>]*>\)//g;/^[[:space:]]*$/d'  file.html


例如:要从下列代码中去除"[]"及其中包括的代码
[b:4c6c2a6554][color=red:4c6c2a6554]一. 替换[/color:4c6c2a6554][/b:4c6c2a6554]

sed 's/\[[^]]\{1,\}\]//g' filename


匹配日期:
Month, Day, Year [A-Z][a-z]\{3,9\}, [0-9]\{1,2\}, [0-9]\{4\}

2003-01-28 或 2003.10.18 或 2003/10/10 或 2003 10 10

\([0-9]\{4\}[ /-.][0-2][0-9][ /-.][0-3][0-9]\)

匹配IP地址
\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)

\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\)


匹配数字串
[-+]*[0-9]\{1,\} 整数

[-+]*[0-9]\{1,\}\.[0-9]\{1,\}  浮点数


从字串中解析出两个子串(前2各字符和后9个字符) 
echo "WeLoveChinaUnix"|sed -e 'H;s/\(..\).*/\1/;x;s/.*\(.\{9\}\)$/\1/;x;G;s/\n/ /' 

We ChinaUnix 


分解日期串 
echo 20030922|sed 's/\(....\)\(..\)\(..\)/\1 \2 \3/'|read year month day 

echo $year $month $day 


文件内容倒序输出
sed '1!G;h;$!d'  oldfile >newfile

阅读(963) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~