I have a csv export where i get some info. Of the 4 lines below line 1 and 2 are fine.
lines 3 and 4 need to have the "2 x " stripped out and also with line 4 I only want the name henrietta anything after the comma (including the comma) to be omitted.
Id like to use a wildcard in place of the number
Linda
bobbyjoe
2 x ash
3 x Henrietta,suzyann,waynebruce,parkerpeter
Currently im using as below. which only, I havent solves part of the problem I havent found an answer online that I understand.
$filepath = "c:\mycsv.csv"
$bad2 = "2 x "
$good2 = `b
$bad2 = "3 x "
$good2 = `b
get-content $filepath | % { $_ -replace $bad2 , $good2 } | % { $_ -replace $bad3 , $good3 } | set-content $saveloc\tidied-$date.csv
Are you familiar with Regular Expressions? See the tag wiki for regex , and have a look at Reference - What does this regex mean?
The -replace
operator in PowerShell is using a regex replacement so it's very easy to replace "(any digit)(space)(literal x)(space)":
$bad2 = '\d x '
# or
$bad2 = '[0-9] x '
# better
$bad2 = '^\d x '
get-content $filepath | % { $_ -replace $bad2, $good2 }
The better version anchors the match to the beginning of the string; that's what the caret ^
does.
Regular expressions are very powerful and PowerShell has excellent support for them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.