I'm trying to write a part of a script that replaces a match line with RegEx.
Here's what the input looks like:
Name, type, ADDRESSES
“Aaa”, “bbb”, “19 S 149TH $NEWPORT NEWS, WA 96332”
“Aaa”, “bbb”, “851 16TH AVE #365$SALISH, WA 98402-4410”
“Aaa”, “bbb”, “2445 E BROADWAY #204$YELM WA 98653”
Here's what I've tried
$regex = '\d{5}([ \-]\d{4})?'
##get the data
$people = Get-Content 'C:\test.csv'
## let's convert the data first
foreach ($p in $people) {
if ($p -match $regex) { $p | out-file -append C:\test.csv }
}
Here's what I expect back
Name, type, ADDRESSES
“Aaa”, “bbb”, “96332”
“Aaa”, “bbb”, “98402-4410”
“Aaa”, “bbb”, “98653”
Here's what I get back:
Name, type, ADDRESSES
“Aaa”, “bbb”, “19 S 149TH $NEWPORT NEWS, WA 96332”
“Aaa”, “bbb”, “851 16TH AVE #365$SALISH, WA 98402-4410”
“Aaa”, “bbb”, “2445 E BROADWAY #204$YELM WA 98653”
$p -match $regexis just going to check if the line matches and then you output the whole line ($p) to file as-is. You are not manipulating the line first.Import-CsvOR if you stick withGet-Contentthen you need to update your regex and use-Replace