Home > Enterprise >  Powershell question - How can I deleted lines from a certain position?
Powershell question - How can I deleted lines from a certain position?

Time:12-01

(Sorry in advance for my bad english.)

I have 2 questions:

  1. Question: I have a .txt file and search with powershell for a word into the .txt:

My code: Select-String .\example.txt -pattern "myword" | select -ExpandProperty line

OK nice. PowerShell found the word within line 63. After line 63, the .txt file has x more lines, and all of (64) them should be deleted. How can this be done?

get-content .\example.txt | select-string -pattern 'myword' -notmatch | Out-File .\new_example.txt

This deleted only line 63. However, this should remain and only the rest should be deleted.

  1. Question: Almost the same. I search for two countings Example: 12 and 33 into the .txt Powershell found not 12a and 33a. That's right. Powershell should for it "11a until 56a", and clear line "10a" and "123a"

Example .txt file: 10a, 11a, 21a, 20w, 32a, 56a, 123a,

That is desired .txt file: 11a, 21a, 20w, 32a, 56a,

How can I make it? I hope so mutch that you can help me by this problems. Thanks.

CodePudding user response:

Using the proposed prototype in #15136 Add -From and -To parameters to Select-String:

@'
One
Two
myword
three
'@ -split '\r?\n' |SelectString -To myword

returns

One
Two

'10a', '11a', '21a', '20w', '32a', '56a', '123a' |
    SelectString -From '(?=11a)' -To '(?<=56a)'

Returns

11a
21a
20w
32a
56a

See also:

  • Related