Windows Vista Forums

Select-String -exclude
  1. #1


    Paul Farrimond Guest

    Select-String -exclude

    I am trying to using Select-String to exclude a line from a CSV file,

    e.g.
    My CSV file looks like this (I have it in a txt file called test.txt):
    Test.txt
    User,FirstName,LastName,DateofBirth
    User1,John,Doe,010156
    User2,Bob,Doe,050679
    User3,Simon,Black,060167
    User4,Liza,Green,030886

    And I would like to remove User3 so I run the following:

    gc Test.txt | select-string -pattern "," -exclude "User3"

    However my output contains all the users still,

    please could someone help me out as to why this doesn't work????



      My System SpecsSystem Spec

  2. #2


    Duncan Smith Guest

    Re: Select-String -exclude

    On Aug 6, 10:23*am, Paul Farrimond wrote:

    > I am trying to using Select-String to exclude a line from a CSV file,
    >
    > e.g.
    > My CSV file looks like this (I have it in a txt file called test.txt):
    > Test.txt
    > User,FirstName,LastName,DateofBirth
    > User1,John,Doe,010156
    > User2,Bob,Doe,050679
    > User3,Simon,Black,060167
    > User4,Liza,Green,030886
    >
    > And I would like to remove User3 so I run the following:
    >
    > gc Test.txt | select-string -pattern "," -exclude "User3"
    >
    > However my output contains all the users still,
    >
    > please could someone help me out as to why this doesn't work????
    Alternatively,

    cat test.txt |? {-not($_ -match "User3")}

    Regards,

    Duncan

      My System SpecsSystem Spec

  3. #3


    Duncan Smith Guest

    Re: Select-String -exclude

    On Aug 6, 11:23*am, Duncan Smith <DSmith1...@xxxxxx> wrote:

    > On Aug 6, 10:23*am, Paul Farrimond wrote:
    >
    >
    >

    > > I am trying to using Select-String to exclude a line from a CSV file,
    >

    > > e.g.
    > > My CSV file looks like this (I have it in a txt file called test.txt):
    > > Test.txt
    > > User,FirstName,LastName,DateofBirth
    > > User1,John,Doe,010156
    > > User2,Bob,Doe,050679
    > > User3,Simon,Black,060167
    > > User4,Liza,Green,030886
    >

    > > And I would like to remove User3 so I run the following:
    >

    > > gc Test.txt | select-string -pattern "," -exclude "User3"
    >

    > > However my output contains all the users still,
    >

    > > please could someone help me out as to why this doesn't work????
    >
    > Alternatively,
    >
    > cat test.txt |? {-not($_ -match "User3")}
    >
    The -exclude parameter in select-string refers to the path (i.e. input
    files like *.txt) not the actual content of the file.

    I suppose you might use a negative look-ahead in the RegEx or
    something, but the above cat statement should do the job...?

    Regards,

    Duncan

      My System SpecsSystem Spec

  4. #4


    Shay Levy [MVP] Guest

    Re: Select-String -exclude

    Hello Paul,


    You can filter the rows you don't need with where-object and export the results
    to a new csv file:

    PS > import-csv test.txt | where {$_.user -ne "User3"} | export-csv newTest.txt





    ---
    Shay Levy
    Windows PowerShell
    http://blogs.microsoft.co.il/blogs/ScriptFanatic

    PF> I am trying to using Select-String to exclude a line from a CSV
    PF> file,
    PF>
    PF> e.g.
    PF> My CSV file looks like this (I have it in a txt file called
    PF> test.txt):
    PF> Test.txt
    PF> User,FirstName,LastName,DateofBirth
    PF> User1,John,Doe,010156
    PF> User2,Bob,Doe,050679
    PF> User3,Simon,Black,060167
    PF> User4,Liza,Green,030886
    PF> And I would like to remove User3 so I run the following:
    PF>
    PF> gc Test.txt | select-string -pattern "," -exclude "User3"
    PF>
    PF> However my output contains all the users still,
    PF>
    PF> please could someone help me out as to why this doesn't work????
    PF>



      My System SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: Select-String -exclude

    "Paul Farrimond" wrote in message
    news:20088652330farrimond.p@xxxxxx

    > I am trying to using Select-String to exclude a line from a CSV file,
    >
    > e.g.
    > My CSV file looks like this (I have it in a txt file called test.txt):
    > Test.txt
    > User,FirstName,LastName,DateofBirth
    > User1,John,Doe,010156
    > User2,Bob,Doe,050679
    > User3,Simon,Black,060167
    > User4,Liza,Green,030886
    >
    > And I would like to remove User3 so I run the following:
    >
    > gc Test.txt | select-string -pattern "," -exclude "User3"
    >
    > However my output contains all the users still,
    >
    > please could someone help me out as to why this doesn't work????
    FYI in version 2 of PowerShelll, Select-String has been updated to include
    a -notmatch parameter. Those of us who use grep -v are very grateful. :-)

    --
    Keith


      My System SpecsSystem Spec

  6. #6


    Brandon Shell [MVP] Guest

    Re: Select-String -exclude

    AMEN!

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    K> "Paul Farrimond" wrote in message
    K> news:20088652330farrimond.p@xxxxxx
    K>

    >> I am trying to using Select-String to exclude a line from a CSV file,
    >>
    >> e.g.
    >> My CSV file looks like this (I have it in a txt file called
    >> test.txt):
    >> Test.txt
    >> User,FirstName,LastName,DateofBirth
    >> User1,John,Doe,010156
    >> User2,Bob,Doe,050679
    >> User3,Simon,Black,060167
    >> User4,Liza,Green,030886
    >> And I would like to remove User3 so I run the following:
    >>
    >> gc Test.txt | select-string -pattern "," -exclude "User3"
    >>
    >> However my output contains all the users still,
    >>
    >> please could someone help me out as to why this doesn't work????
    >>
    K> FYI in version 2 of PowerShelll, Select-String has been updated to
    K> include a -notmatch parameter. Those of us who use grep -v are very
    K> grateful. :-)
    K>
    K> --
    K> Keith



      My System SpecsSystem Spec

  7. #7


    Paul Farrimond Guest

    Remove from the csv file from a list

    Cheers Good Work around.

    .....So I now have a list of users in a seperate text file that I want to exclude from the csv file. E.g.

    List.txt

    User1

    User3

    But if I get-content from a list.txt file with the users and do

    foreach ($a in (gc list.txt)) {import-csv test.txt | where {$_.User -ne $a}}

    It runs through the list.txt and only removes the specified user on each pass so the result ends up being:

    User FirstName LastName DateofBirth
    ---- --------- -------- -----------
    User2 Bob Doe 050679
    User3 Simon Black 060167
    User4 Liza Green 030886
    User1 John Doe 010156
    User2 Bob Doe 050679
    User4 Liza Green 030886

    What would be the best way to achieve having a list of users in list.txt and removing them from a CSV file??

    Cheers

    Paul


      My System SpecsSystem Spec

  8. #8


    Paul Farrimond Guest

    Remove from the csv with a list

    So I now have a list of users in a seperate text file that I want to exclude from the csv file. E.g.

    List.txt contains a list of users:

    User1

    User3

    But if I get-content from a list.txt file with the users and do

    foreach ($a in (gc list.txt)) {import-csv test.txt | where {$_.User -ne $a}}

    It runs through the list.txt and only removes the specified user on each pass so the result ends up being:

    User FirstName LastName DateofBirth
    ---- --------- -------- -----------
    User2 Bob Doe 050679
    User3 Simon Black 060167
    User4 Liza Green 030886
    User1 John Doe 010156
    User2 Bob Doe 050679
    User4 Liza Green 030886

    What would be the best way to achieve having a list of users in list.txt and removing them from a CSV file??

    Cheers

    Paul




      My System SpecsSystem Spec

  9. #9


    Suresh Tadisetty Guest

    Remove from the csv with a list - Paul Farrimond

    I hope this idea works - Update test.txt in every pass with the output of that pass.

    foreach ($a in (gc list.txt)) {import-csv test.txt | where {$_.User -ne $a} > Update-test.txt}




    Paul Farrimond wrote:

    Remove from the csv with a list
    18-Aug-08

    So I now have a list of users in a seperate text file that I want to exclude from the csv file. E.g.

    List.txt contains a list of users:

    User1

    User3

    But if I get-content from a list.txt file with the users and do

    foreach ($a in (gc list.txt)) {import-csv test.txt | where {$_.User -ne $a}}

    It runs through the list.txt and only removes the specified user on each pass so the result ends up being:

    User FirstName LastName DateofBirth
    ---- --------- -------- -----------
    User2 Bob Doe 050679
    User3 Simon Black 060167
    User4 Liza Green 030886
    User1 John Doe 010156
    User2 Bob Doe 050679
    User4 Liza Green 030886

    What would be the best way to achieve having a list of users in list.txt and removing them from a CSV file??

    Cheers

    Paul

    EggHeadCafe - Software Developer Portal of Choice
    Build a Multi-Provider Async Methods Search Page
    http://www.eggheadcafe.com/tutorials...ovider-as.aspx

      My System SpecsSystem Spec

  10. #10


    Vadims Podans [MVP] Guest

    Re: Remove from the csv with a list - Paul Farrimond

    IIUC your task, you may need use this:

    $csv = import-csv test.txt
    foreach ($a in $(gc text.txt)) {
    $csv = $csv | ?{$_.user -ne $a}
    }

    --
    WBR, Vadims Podans
    MVP: PowerShell
    PowerShell blog - www.sysadmins.lv

    "Suresh Tadisetty" rakstija zinojuma
    "news:2009101371618sureshbabu.tadisetty@newsgroup"...

    > I hope this idea works - Update test.txt in every pass with the output of
    > that pass.
    >
    > foreach ($a in (gc list.txt)) {import-csv test.txt | where {$_.User -ne
    > $a} > Update-test.txt}
    >
    >
    >
    >
    > Paul Farrimond wrote:
    >
    > Remove from the csv with a list
    > 18-Aug-08
    >
    > So I now have a list of users in a seperate text file that I want to
    > exclude from the csv file. E.g.
    >
    > List.txt contains a list of users:
    >
    > User1
    >
    > User3
    >
    > But if I get-content from a list.txt file with the users and do
    >
    > foreach ($a in (gc list.txt)) {import-csv test.txt | where {$_.User -ne
    > $a}}
    >
    > It runs through the list.txt and only removes the specified user on each
    > pass so the result ends up being:
    >
    > User FirstName LastName
    > DateofBirth
    > ---- --------- --------
    > -----------
    > User2 Bob Doe
    > 050679
    > User3 Simon Black
    > 060167
    > User4 Liza Green
    > 030886
    > User1 John Doe
    > 010156
    > User2 Bob Doe
    > 050679
    > User4 Liza Green
    > 030886
    >
    > What would be the best way to achieve having a list of users in list.txt
    > and removing them from a CSV file??
    >
    > Cheers
    >
    > Paul
    >
    > EggHeadCafe - Software Developer Portal of Choice
    > Build a Multi-Provider Async Methods Search Page
    > http://www.eggheadcafe.com/tutorials...ovider-as.aspx

      My System SpecsSystem Spec

Select-String -exclude problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with $var | select-string -pattern $string -q Ben Christian PowerShell 3 08 Feb 2008
select-string and .split alicain PowerShell 3 12 Oct 2007
Re: select-string exceptions cmyers PowerShell 0 23 May 2007
Re: select-string exceptions cmyers PowerShell 0 23 May 2007
Exclude lines that include string Brian Hoort PowerShell 4 15 Jan 2007