免费注册 查看新帖 |

Chinaunix

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

能否用grep从@数组中取出包含特定字符的 所有行? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-19 22:41 |只看该作者 |倒序浏览
我用grep从数组中取数据时,发现,即使包含特定字符的行有5行,它也只能取出一行。

我看perl 参考大全里面对map和grep的介绍,都是说只能用于$变量,

那么,

像这样:                @metastat_mirror = grep /Mirror/, @my_metastat; #这个只能取出一行,我想取出所有包含Mirror的行

这个要怎么做到呢?

[ 本帖最后由 jjqing 于 2008-10-19 22:44 编辑 ]

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
2 [报告]
发表于 2008-10-19 23:26 |只看该作者
原帖由 jjqing 于 2008-10-19 22:41 发表
我看perl 参考大全里面对map和grep的介绍,都是说只能用于$变量,

没见过你说的

perldoc -f grep
       grep BLOCK LIST
       grep EXPR,LIST
               This is similar in spirit to, but not the same as, grep(1) and its relatives.  In particular, it is not
               limited to using regular expressions.

               Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the
               list value consisting of those elements for which the expression evaluated to true.  In scalar context,
               returns the number of times the expression was true.

                   @foo = grep(!/^#/, @bar);    # weed out comments

               or equivalently,

                   @foo = grep {!/^#/} @bar;    # weed out comments

               Note that $_ is an alias to the list value, so it can be used to modify the elements of the LIST.  While
               this is useful and supported, it can cause bizarre results if the elements of LIST are not variables.
               Similarly, grep returns aliases into the original list, much as a for loop's index variable aliases the
               list elements.  That is, modifying an element of a list returned by grep (for example, in a "foreach",
               "map" or another "grep") actually modifies the element in the original list.  This is usually something to
               be avoided when writing clear code.

               See also "map" for a list composed of the results of the BLOCK or EXPR.

论坛徽章:
0
3 [报告]
发表于 2008-10-19 23:35 |只看该作者

回复 #1 jjqing 的帖子

@metastat_mirror = grep /Mirror/, @my_metastat;
这个就能取出所有行,perl里的grep我用过无数次了...

论坛徽章:
0
4 [报告]
发表于 2008-10-20 09:23 |只看该作者
不好意思,原来是我的程序中,前面有一行代码将@my_metastat做了一个shift,所以刚好把一个包含Mirror的行去掉了,现在改过来就可以了:
以前是:
                chomp ($my_metastat = shift(@my_metastat));
现在改成:
                @my_result = @my_metastat;
                chomp ($my_metastat = shift(@my_result));

再运行程序,输出就正常了:
                @metastat_mirror = grep /Mirror/, @my_metastat;
                print "\@metastat_mirror is \n @metastat_mirror \n";
====================
@metastat_mirror is
d40: Mirror
d30: Mirror

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
5 [报告]
发表于 2008-10-20 09:47 |只看该作者
原帖由 jjqing 于 2008-10-20 09:23 发表
                @my_result = @my_metastat;
                chomp ($my_metastat = shift(@my_result));

从贴出的代码看不出这两句是干嘛的(取出第一个给$my_metastat ),不过确实不会影响后面的 @metastat_mirror = grep /Mirror/, @my_metastat;

论坛徽章:
0
6 [报告]
发表于 2008-10-20 10:23 |只看该作者
原帖由 ynchnluiti 于 2008-10-20 09:47 发表

从贴出的代码看不出这两句是干嘛的(取出第一个给$my_metastat ),不过确实不会影响后面的 @metastat_mirror = grep /Mirror/, @my_metastat;



从@my_metastat中取出第一个给$my_metastat,是用来判断是否存在有metastat这个命令的输出,这个命令的输出是这样:
如果有配置meta设备,则metastat显示meta设备的信息,否则就输出为“there are no existing databases",同时,因为我的程序支持telnet模式,所以我也要检查一下,这个数组的第一行,是否包含"No such file or directory"这种输出,最后,如果主机上根本没有metastat这个命令,我也要检查一下这个数组的第一行,是否包含"command not found"这种输出,如下:

                @my_result = @my_metastat;
                chomp ($my_metastat = shift(@my_result));

                if ( ($my_metastat =~ m/No such file or directory/)  || (!$my_metastat) || ($my_metastat =~ m/no existing databases/) || ($my_metastat =~ m/command not found/)){
                        print MYFILE "<font color=#6600FF>Your metastat hasn't contents, I can't checkup!</font><HR><P> \n";
                        print MYFILE "<P><A class='awr' HREF='#top'>Back to Top</A><HR><P> \n";
                }        else        {
                        #执行语句
                        & sun_svm_mirror;
                }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP