I am really very new to powershell. I want to use powershell to read a txt file and change it to another format.
and then write the output to a new file.
I just started powershell two days ago so I don't know how to do this yet.
Reading from a file:
Get-Content file.txt
Not quite sure what you want here. Get-Content
returns an array of strings. You can then manipulate what you get and pass it on. The most helpful cmdlets here are probably Where-Object
(for filtering) and ForEach-Object
(for manipulating).
For example, to remove all blank lines you can do
Get-Content file.txt | Where-Object { $_ -ne '' } > file2.txt
This can be shortened to
Get-Content file.txt | Where-Object { $_ } > file2.txt
since an empty string in a boolean context evaluates to false
.
Or to remove spaces in every line:
Get-Content file.txt | ForEach-Object-Object { $_ -replace ' ' } > file2.txt
Again, not quite sure what you're after here. Possible things I could think of from your overly elaborate description are something along the lines of
$_.Substring(2).Length
or
$_ -match '(\d+)' | Out-Null
$Matches[1].Length
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With