user664481
user664481

Reputation: 2301

Regular expression in Powershell to remove unwanted string

Hi how to write a regular expression in powershell to remove any string that started with | D|Ref.Doc. ?

| D|Ref.Doc.  |Row|DocumentNo|CoCd|Pstng Date|Period
| W|100003574 |  3|65697957  |CACS|01/15/2016|     1
| W|100003574 |  3|65697957  |CACS|01/15/2016|     2
| W|100003574 |  3|65697957  |CACS|01/15/2016|     3
| D|Ref.Doc.  |Row|DocumentNo|CoCd|Pstng Date|Period
| W|100003575 |  3|65697957  |CACS|01/15/2016|     1
| W|100003575 |  3|65697957  |CACS|01/15/2016|     2
| W|100003575 |  3|65697957  |CACS|01/15/2016|     3

Expected output

| W|100003574 |  3|65697957  |CACS|01/15/2016|     1
| W|100003574 |  3|65697957  |CACS|01/15/2016|     2
| W|100003574 |  3|65697957  |CACS|01/15/2016|     3
| W|100003575 |  3|65697957  |CACS|01/15/2016|     1
| W|100003575 |  3|65697957  |CACS|01/15/2016|     2
| W|100003575 |  3|65697957  |CACS|01/15/2016|     3

Upvotes: 0

Views: 153

Answers (1)

mklement0
mklement0

Reputation: 438153

Try this:

Get-Content $path | ? { $_ -notmatch '^\| D\|Ref\.Doc\.' }

Note how | and . need to be \-escaped in order to be interpreted as literals.
^ anchors the regular expression at the start of each input line.

However, as @PetSerAl suggests in a comment, instead of using a regular expression, you can get away with a simpler wildcard pattern in this case:

Get-Content $path | ? { $_ -notlike '| D|Ref.Doc.*' }

Note that ? is an alias for the filtering Where-Object cmdlet.

Upvotes: 1

Related Questions