免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3396 | 回复: 15
打印 上一主题 下一主题

sed实例求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-06 23:08 |只看该作者 |倒序浏览
最近在学shell,在练习sed命令时想到一个问题

比如httpd.conf文件

</IfModule>

#
# "/usr/local/apache/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/usr/local/apache/cgi-bin">
    AllowOverride None
    Options None
    #Order allow,deny
    #Allow from all
</Directory>

#
# DefaultType: the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType text/plain



下述二种情况的内容需要删除

1) 空行
2) 被注释的行内容, 这里包括#开头的,或者空格,TAB后马上接#的

试了好久,小弟只能通过两次命令才能完成,

$ sed '/^[[:space:]]*$/d' httpd.conf | sed '/^[[:space:]]*#/d' -


怎样才能一次匹配就能解决上面2种情况呢?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2009-12-06 23:49 |只看该作者
一次正则太难了。你的意思就是不想用管道吧?
sed '/^[[:space:]]*$/d;/^[[:space:]]*#/d' httpd.conf

论坛徽章:
0
3 [报告]
发表于 2009-12-06 23:53 |只看该作者
恩,原意是不想用管道,已经是改进了,当然能一次正则当然是最好了

学到东西了

论坛徽章:
0
4 [报告]
发表于 2009-12-07 09:17 |只看该作者
sed '/^#/d;/\s*#/d' file

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
5 [报告]
发表于 2009-12-07 10:19 |只看该作者

回复 #1 zsj28 的帖子

try:

sed '/^[[:space:]]*\(#.*\)\{0,1\}$/d' httpd.conf

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2009-12-07 10:32 |只看该作者

回复 #5 ly5066113 的帖子

学到东西了

论坛徽章:
0
7 [报告]
发表于 2009-12-07 11:10 |只看该作者
学习了

论坛徽章:
0
8 [报告]
发表于 2009-12-07 11:38 |只看该作者
原帖由 ly5066113 于 2009-12-7 10:19 发表
try:

sed '/^[[:space:]]*\(#.*\)\{0,1\}$/d' httpd.conf

简化下 sed -r /^[[:space:]]*(#.*)?$/d'

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
9 [报告]
发表于 2009-12-07 12:28 |只看该作者
awk '!NF==0 &&! ( $1~/^#/)' httpd.conf

[ 本帖最后由 rdcwayx 于 2009-12-7 12:30 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2009-12-07 14:07 |只看该作者
发个弱智的
sed -i -e '/^\#/d' -e '/^$/d' -e '/^\    #/d' httpd.conf
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP