(Sorry in advance for my bad english.)
I have 2 questions:
- 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.
- 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:
- Extract specific text and extract
- How to select between multiple lines in power shell?
- How to read lines between 2 special characters in txt-file with Powershell
- Unable to get data between multiple keyword using powershell
- Need Regex to match multiple lines until Match is found between common delimiters
- Extract multiple lines of text between two key words from shell command in powershell
- Extract block of text using regex and powershell
- Need to search a line from a long config file with powershell
- Powershell Regex: Reading a multi-line string between two points