0

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
2
  • -notlike'| D|Ref.Doc.*' Commented Feb 20, 2016 at 4:37
  • How to use -notlike in this command ? $d = Get-Content -path $path Commented Feb 20, 2016 at 5:04

1 Answer 1

1

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.

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

1 Comment

Thanks for the command. 1 more question how to add or condition into command Get-Content $path | ? { $_ -notmatch '^\| D\|Ref\.Doc\.' } ? for example I want to exclude any line starting at | D\|Ref\.Doc\. or | |

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.