Pergunta

I have the following code:

$reader = [System.IO.File]::OpenText(".\Babytest.txt")
try {
    for(;;) 
    {
    $line = $reader.ReadLine()
    if($line eq $null) {break}
            if($line -notMatch 'BB1') { $line }


    }
}
finally {
    $reader.Close()
}

The way I understand this code, is:

$reader = [System.IO.File]::OpenText(".\Babytest.txt")

-- Opens the test.txt file

for(;;) 
        {
        $line = $reader.ReadLine()
        if($line eq $null) {break}
                 if($line -notMatch 'BB1') { $line }

-- If the line is null= EOF break code Else go into another IF look for all lines where there is no 'BB1' in it, output $line

What I want to do

First of all, this code works, although the IF - notmatch doesn't seem to be working, I still see the lines with BB1 in them in my output to the shell. What am I missing?

How can I write this out to a file? This is kind of more loopy stuff oppose to the pipe stuff, so I'm not sure if something like

|out-file "\\fhnsrv01\home\aborgetti\Documentation\Scripts\Baby Removal Project\babyoutput.txt"

would work...all help is greatly appreciated... I've been researching this all day, but it seems powershell has a learning curve, I'm getting the hang of it, there's just so much you can do...

UPDATE

Example String:

AR|111000111011|500001|1|DavT|2013-09-10 12:03:18|2014-04-07 10:23:09|A25415|3|2013-08-11|2013-08-13|Y|01 |APPROVED|01 |APPROVED|35 |

String with no BB1

AR|111100001BB1|500002|1||2014-04-02 15:30:12|2014-04-04 10:55:54|A32009|3|2014-03-31|2014-04-02|Y|01 |APPROVED|03 |PENDING|34 |

String with BB1

I want to remove the lines like BB1 line

Foi útil?

Solução

Using a for loop and a StreamReader to read a text file, although not wrong, is just not a right way to do things in Powershell. Unless you have some kind of critical performance requirement (in which case you shouldn't be scripting), it's much better to use the high-level tools that Powershell affords you.

Get-Content .\Babytest.txt |?{ $_ -notmatch 'BB1' } | Out-File 'BabyOutput.txt'

This should do what you want.

Outras dicas

Try this. It uses native powershell commands, and will actually out-perform your stream reader on large files:

Get-Content .\Babytest.txt -ReadCount 1000 |
 foreach {
  $_ -notmatch 'BB1' |
  Add-Content '\\fhnsrv01\home\aborgetti\Documentation\Scripts\Baby Removal Project\babyoutput.txt'
}

Edit: Using the posted test data-

'AR|111000111011|500001|1|DavT|2013-09-10 12:03:18|2014-04-07 10:23:09|A25415|3|2013-08-11|2013-08-13|Y|01 |APPROVED|01 |APPROVED|35 |',
'AR|111100001BB1|500002|1||2014-04-02 15:30:12|2014-04-04 10:55:54|A32009|3|2014-03-31|2014-04-02|Y|01 |APPROVED|03 |PENDING|34 |'|
set-content ./babytest.txt

Get-Content .\Babytest.txt -ReadCount 1000 |
 foreach {
  $_ -notmatch 'BB1'
}

AR|111000111011|500001|1|DavT|2013-09-10 12:03:18|2014-04-07 10:23:09|A25415|3|2013-08-11|2013-08-13|Y|01 |APPROVED|01 |APPROVED|35 |

I would do it like this, but what do I know:

while (($line = $reader.ReadLine()) -ne $null) {
    if($line -notlike "*BB1*")
    {
        $line
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top