0

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

1 Answer 1

4

Are you familiar with Regular Expressions? See the tag wiki for , 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Never heard of it until now. Ive implemented that and it works great. Many Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.