Question:
What would be the command to remove everything after a string (\test.something).
I have information in a text file, but after the string there is like a 1000 lines of text that I don’t want. how can I remove everything after and including the string.
This is what I have – not working. Thank you so much.
1 2 3 4 |
$file = get-item "C:\Temp\test.txt" (Get-Content $file) | ForEach {$_.TrimEnd("\test.something\")} | Set-Content $file |
Answer:
Why remove everything after? Just keep everything up to it (I’m going to use two lines for readability but you can easily combine into single command):
1 2 3 4 |
$text = ( Get-Content test.txt | Out-String ).Trim() #Note V3 can just use Get-Content test.txt -raw $text.Substring(0,$text.IndexOf('\test.something\')) | Set-Content file2.txt |
Also, you may not need the Trim but you were using TrimEnd so added in case you want to add it later. )