在一行上匹配多个参数

时间:2014-04-03 09:37:29

标签: powershell match

我读了一个文件并搜索了一些包含“已转移”字样和CODES“ZEN”和“ROK”的特定行。 我的脚本应该只选择具有这三个参数的行但是ROK不应该是星号*

这是一个例子和我写的条件:

行:

Transfered    TWIST=1 ROK=TOTO      ZEN=C2813310

Transfered    TEST2=3 ROK=*         ZEN=C2813310

Transfered    CALIB=7 ROK=*         ZEN=C2813310

Transfered    LIKR1=9 ROK=TOTO      ZEN=C2813310

我的if条件以匹配元素:

$content = get-content "c:\test\log"    

foreach ($line in $content) {
    if ($line -match "Transfered.*ZEN=(.+?)\b.*")

我的问题是如何将“ROK”参数添加到相同的“if”条件,它不应该等于*星。

所以,在脚本的最后,从上面的例子中,我应该只有这两行,其中ROK不等于*

Transfered    TWIST=1 ROK=TOTO      ZEN=C2813310

Transfered    LIKR1=9 ROK=TOTO      ZEN=C2813310

我的脚本还执行其他任务,但这些任务不基于我必须应用的此过滤器。

提前谢谢。

ps:我试过这个,但没有用:

if ($line -match "Transfered.*ZEN=(.+?).*ROK=(.+?)\b.*")

2 个答案:

答案 0 :(得分:0)

如果我理解正确,这就足够了:

if ($line -match "Transfered.*ROK=[^\*].*ZEN")

答案 1 :(得分:0)

试试这个:

$data = @'
I read a file and I search some specific lines that contains the word "transfered" and the CODES "ZEN" and "ROK". My script should select only the lines with these three parameters BUT the ROK should not be a star character "*".
Here an example and the condition that I wrote:
lines:
Transfered TWIST=1 ROK=TOTO ZEN=C2813310
Transfered TEST2=3 ROK=* ZEN=C2813310
Transfered CALIB=7 ROK=* ZEN=C2813310
Transfered LIKR1=9 ROK=TOTO ZEN=C2813310
My if condition in order to match the elements:
'@.split("`n")

$data | ? { $_ -match 'Transfered.*ROK=[^*]+ZEN.*' } | % {$_}
相关问题