Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract rows from csv

Status
Not open for further replies.

Nigelh53

Programmer
Aug 1, 2018
1
GB
Hi
I'm trying to extract rows that match a pattern (3 characters and 8 digits) from a csv file and output them to a new csv
Code:
#Import the CSV file with all the data.
$csv = Import-CSV J:\CanvasData\users_utf8nobom.csv 
#Process the CSV file one row at a time 
foreach ($row in $csv) { 
#Each column is accessed using the column header. 
#We can use the -not operator to specify 'doesn't match'.      
if ($row.user_id -eq "\b[\D[3}\d{8}]\b")
{ 
#... Export that row to a new CSV file.        
$row | Export-CSV J:\Temp\teacherlist.csv  -Append -NoTypeInformation   
 } 
}

an example of the csv file is below
user_id status
MCN18158364 active
901658 active
W00054 active
MCN17125917 active
 
I am far from expert, but...

How about -match rather than -eq?

Tom Morrison
Consultant
 
\b[\D[highlight #FCE94F][[/highlight]3}\d{8}]\b"

... is that correct?

Just my $.02

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
should probably be \b\D{3}\d{8}\b

if ($row.user_id -match "\b\D{3}\d{8}\b")

Yeah.... the addition of the braces [] would say "any match of the characters in the braces" I believe...
I think your match should work....



Just my $.02

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top