Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Select-String -exclude

175 views
Skip to first unread message

paulfarrimond

unread,
Aug 6, 2008, 5:23:29 AM8/6/08
to
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????

Duncan Smith

unread,
Aug 6, 2008, 6:23:40 AM8/6/08
to

Alternatively,

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

Regards,

Duncan

Duncan Smith

unread,
Aug 6, 2008, 6:30:12 AM8/6/08
to

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

Shay Levy [MVP]

unread,
Aug 6, 2008, 7:43:13 AM8/6/08
to
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>


Keith Hill [MVP]

unread,
Aug 6, 2008, 10:41:53 AM8/6/08
to
"Paul Farrimond" wrote in message
news:20088652330...@runshaw.ac.uk...

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

Brandon Shell [MVP]

unread,
Aug 6, 2008, 10:56:18 AM8/6/08
to
AMEN!

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

K> "Paul Farrimond" wrote in message
K> news:20088652330...@runshaw.ac.uk...
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


paulfarrimond

unread,
Aug 18, 2008, 11:18:15 AM8/18/08
to
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

paulfarrimond

unread,
Aug 18, 2008, 11:35:15 AM8/18/08
to
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:

sureshtadisetty

unread,
Oct 13, 2009, 7:16:14 AM10/13/09
to
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

User1

User3

Cheers

Paul

EggHeadCafe - Software Developer Portal of Choice
Build a Multi-Provider Async Methods Search Page
http://www.eggheadcafe.com/tutorials/aspnet/10697ee3-bc13-4ca4-9443-988bfcbaaacc/build-a-multiprovider-as.aspx

Vadims Podans [MVP]

unread,
Oct 13, 2009, 8:32:23 AM10/13/09
to
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:2009101371618sur...@gmail.com"...

0 new messages