脚本中的正则表达式问题??? [英] Problem with regex in the script ???

查看:65
本文介绍了脚本中的正则表达式问题???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在此先感谢..


问题陈述:需要显示原始统计文件报告,但脚本计数stat'的文件有* .xls


从目录中读取输入的原始统计文件&比较统计类型和&把它推到阵列然后稍后

计算出现率。


现在问题是如果该目录包含原始统计数据excel文件,它会读取该文件&增加不正确的计数。


我只需打印原始统计数据文件计数(没有任何文件类型扩展名的文件)

如何实现这一点???


i知道问题与正则表达式如果脚本中的stmts


目录内容如下:

prstat-Ls-20080118-1800

prstat-Ls-20080118 -1900

prstat-Ls-20080118-1900.xls

prstat-Lvs-20080118-1800

prstat-Lvs-20080118-1900



输出应如下所示:

C中共有4个文件:\ Performance_vap \INPUT_FILES \

共有2个prstat_Ls文件存在(即使我们有prstat * .xls文件...脚本应丢弃此文件)

共有2个prstat_Lvs文件



但是我得到了这个输出:

C中共有5个文件:\ Performance_vap \INPUT_FILES \

那里总共有3个prstat_Ls文件存在(即使我们有prstat * .xls文件..脚本也计算* .xls文件)

共有2个prstat_Lvs文件存在


根据脚本,我们得到的输出是正确的,但我只是希望脚本只计算原始文件但不计算* .xls

如何做到这一点???


Plz任何人都可以帮我这个???


脚本是这样的:

Hi All,

Thanks in Advance..

Problem statement: Need to display the raw stats file report but script count the stat''s file which has *.xls

Am reading the input raw stats files from a directory & comparing the stats type & pushing it to array then later
counting the occurance.

now the problem is if that directory contains raw stats excel file, it reads that file & increment the count which is not correct.

all i need to print only raw stats file count (file which doesn''t have any file type extension)

how to achieve this ???

i know problem is with regex "if" stmts in the script

directory content look like this:
prstat-Ls-20080118-1800
prstat-Ls-20080118-1900
prstat-Ls-20080118-1900.xls
prstat-Lvs-20080118-1800
prstat-Lvs-20080118-1900


Output should look like this:
There are totally 4 files present in C:\Performance_svap\INPUT_FILES\
There are totally 2 prstat_Ls files present (even though we have prstat*.xls file..script should discard this file)
There are totally 2 prstat_Lvs files present


But i get this output:
There are totally 5 files present in C:\Performance_svap\INPUT_FILES\
There are totally 3 prstat_Ls files present (even though we have prstat*.xls file..script counted *.xls file too)
There are totally 2 prstat_Lvs files present

As per the script what ever the ouput am getting is correct but i just want script to count only raw files but not the *.xls
how to do this ???

Plz can anyone help me on this ???

Script goes like this:

展开 | 选择 | Wrap | 行号

推荐答案

展开 | 选择 | Wrap | 行号


谢谢Kevin !!!!


想要实现哈希正如你所说的那样......

但我还在学习perl,因为杰夫告诉我在另一篇帖子中通过哈希法...


i will如果你可以帮助我如何实现哈希方法来计算文件,那就太棒了。

一个例子对我来说就足够了,或者只是一步一步解释..

i想尝试自我..请告诉我怎么去头......

希望你不介意......


无论如何'再次感谢'...


问候,

Vijayarl
Thanks Kevin !!!!

Would like to implement hash as you said...
but am still learning perl, as jeff told me to go through the hash method in my another post...

i will be very greatful if you can assist me on how to implement hash method to count the files..
one example would be sufficient for me or just explaination step by step..

i would like to try by self..just tell me how to go head...
hope you won''t mind...

anyway''s thanks once again...

Regards,
Vijayarl


这是一个一般重写您的代码,包括使用哈希来存储计数和其他更改。值得注意的是,如果/ elsif / elsif而不是if / if / if。当一个字符串或一行只能有一个真值时,不要使用if / if / if perl必须评估所有'if'条件,即使它找到唯一的真值。 if / elsif使perl能够在找到第一个真值后停止执行条件。如果你曾经遇到过这种情况,你可以在最后添加一个其他条件来捕获异常。在你的情况下,没有必要我可以看到一个通过条件。我还清理了你的正则表达式,主要是为了向你展示编写它们以检查模式的方法。你真的在检查子字符串而不是模式,在这种情况下,index()比正则表达式更好用。但是因为我们想要捕获模式匹配的值并将其用作散列键,所以我使用了纯正的regexp而不是index()和预定义的键,这也是一种很好的方法来完成你正在做的事情。


未经测试的代码:

Here is a general rewrite of your code including using a hash to store the counts and other changes. Notably if/elsif/elsif instead of if/if/if. When a string or line can have only one true value don''t use if/if/if as perl has to evaluate all the ''if'' conditions even after it finds the only true one. if/elsif enables perl to stop executing the conditions after the first true value if found. If you ever neeeded a fall-through condition you add an ''else'' condition to the end to catch exceptions. In your case there is no need that I can see for a fall-through condition. I also cleaned up your regexp, mostly just to show you ways of writing them to check for patterns. You were really checking for substrings instead of patterns, in which case index() would have been better to use than regular expressions. But since we want to capture the value of the pattern match and use it as the hash key I went with pure regexps instead of index() and predefined keys, which is also a good possible way to do what you are doing.

Untested code:

展开 | 选择 | Wrap | 行号


这篇关于脚本中的正则表达式问题???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆