0

您好,我正在尝试提取一个以目标“值”开头的字符串,然后将提取其间的所有信息,然后当它看到相同的标记“值”时,它将停止提取。我正在处理一个txt文件。

前任:

80 E4 00 80 09 4F 07 A0 00 00 01 51 53 7F 00 A4 04 00 00 80 E6 02 00 22 09 A0 00 00 00 18 20 41 43 54 00 14 AD 15 C6 80 E8 00 00 C0 E2 01 00 00 18 10 01 08 01 00 00 80 E8 00 01 C0 A0 00 00 00 18 20 01 04 05 00 02 08 A0 00 00 80 E8 00 02 C0 FF 00 01 00 00 00 07 0A C7 7 06 01 0 8 F E6 0C 00 4E 09 A0 00 00 00 18 20 41 43 54 0A A0 00 00 00 18 20 80 E8 02 00 21 08 A0 00 00 00 18 10 01 07 00 14 4F 7E 23 A9 73 80 E6 02 00 00 18 10 01 07 09 A0 00 00 00 18 10 80 C0 00 00 01 00 00 A4 04 00 0F A0 00 00 00 18 10 01 08 01 00 00 00 BA FE 02

所以我想从它从“80 E6”开始的地方提取,然后提取其间的所有信息,直到它再次显示“80 E6”的标签。

所以输出将是:

80 E6 02 00 22 09 A0 00 00 00 18 20 41 43 54 00 14 AD 15 C6 80 E8 00 00 C0 E2 1B 4F 0F A0 00 00 00 18 10 01 08 01 00 00 80 E8 00 01 0 1 C0 A0 20 01 04 05 00 02 08 A0 00 00 80 E8 00 02 C0 FF 00 01 00 00 00 07 0A C7 06 00 F3 80 77 01 80 E6 0C 00 4E 09 A0 00 00 00 180A 20 41 0 43 0 4 18 20 80 E8 02 00 21 08 A0 00 00 00 18 10 01 07 00 14 4F 7E 23 A9 73

到目前为止,我有这个作为我的脚本:

 Function Extractor(){

$firstTag = '^80 E6.*$'
$secondTag = '^80 E6.*$'

$t= foreach ($line in (Get-Content C:\Users\user\Desktop\dummy.txt )) { 

$x
    if ($line -match $firstTag) {

        do {             

            $foreach.current 


            $foreach.MoveNext() > $null

        } 

        until ($foreach.current -match $secondTag)         

        $foreach.current 

#        continue

        Write-Verbose "Next match"

    }
}
$t > C:\Users\nicalder\Desktop\Project1\MasterCard_Trace\modMasterCardTraceExtracted2lined.txt

}
Extractor

我是不是做错了什么,因为这在其他情况下对我很有用..

谢谢!

4

1 回答 1

0

通过一些输入,您所拥有的内容将“从文件末尾脱落”。添加到下面“直到”子句的测试解决了这个问题。

这与输入的输出相匹配。我不得不对如何在完整案件中处理案件做出一些假设。

Function Extractor{

    $firstTag = '^80 E6.*$'

    $secondTag = '^80 E6.*$'



    $t= foreach ($line in (Get-Content C:\temp\input3.txt)) { 

        if ($line -match $firstTag) {

            $firstMatch = $line

            $foreach.MoveNext() > $null

            $lineCounter = 2

            do  {             

                if ($lineCounter -gt 2) {

                    if ($linecounter -eq 3) {

                        $firstMatch

                         $secondMatch

                    }

                    $foreach.current 

                }

                $secondMatch = $foreach.current # only used for the second line,     otherwise its is thrown away

                $foreach.MoveNext() > $null

                $lineCounter++

           }

           until (($foreach.current -match $secondTag) -or ($foreach.current -eq    $null))

#                $foreach.current 

    #        continue

        }

    }

    $t | Out-file "C:\temp\output3.txt"

 }

Extractor
于 2012-07-23T14:38:22.333 回答